3 ms·
I really really want lazy imports in Python, it's would be a godsend for CLIs
by patrick91 1y ago
I really really want lazy imports in Python, it's would be a godsend for CLIs
- nomel 1y agoLibraries for this have always existed, triggering import on first access. The problem was, they would break linters. But that's not an issue anymore with typing.TYPE_CHECKING. A PEP is very much welcome, but using lazy import libraries is a fairly common, very old, method of speeding things up. My pre PEP 690 code looks like this: import typing from lazy import LazyImport member = LazyImport('my_module.subpackage', 'member') member1, member2, = LazyImport('my_module', 'member1', 'member2') if typing.TYPE_CHECKING: # normal import, for linter/IDE/navigation. from my_module.subpackage import member from my_module import member1, member2
- formerly_proven 1y agoWell if you use argparse or one of the many argparse wrappers for a moderately complex CLI you end up lazyfing the CLI parser itself because just fully populating the argparse data structures can easily take half a second or more, so with other startup costs you easily end up with "program --help" taking >1s and any CLI parsing error also taking >1s.