Re:HRT's Python Fork: Leveraging PEP 690 for Faster Imports
by patrick91 on Friday August 08, 2025 @02:43PM
(#44840278)
I really really want lazy imports in Python, it's would be a godsend for CLIs
[ Parent
| on HN ]
Re:HRT's Python Fork: Leveraging PEP 690 for Faster Imports
by nomel on Friday August 08, 2025 @03:51PM
(#44840980)
Libraries 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
Re:HRT's Python Fork: Leveraging PEP 690 for Faster Imports
by formerly_proven on Friday August 08, 2025 @03:58PM
(#44841044)
Well 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.