Live data from Hacker News

PEP 810 – Explicit lazy imports

peps.python.org

1–10 of 247 posts

Re: PEP 810 – Explicit lazy imports

#7
post #3

I wonder how much things would break if all imports were lazy by default.

Maybe nothing would break ? edit: ok well "xxx in sys.modules" would indeed be a problem

Yeah unfortunately in real world Python code people put side effects in their modules all the time.

Re: PEP 810 – Explicit lazy imports

#9
post #2

Feels like a good feature, with a simple explanation, real world use cases, and a scoped solution (global only, pretty simple keyword). I like it!

Yeah, I think this is one of the cleanest PEPs to come around in quite a while, at least from the userspace perspective. Interested to see what happens after the traditional syntax bikeshedding ritual has been completed.

Re: PEP 810 – Explicit lazy imports

#10
If anyone's interested I've implemented a fairly user friendly lazy import mechanism in the form of context managers (auto_proxy_import/init) at https://pypi.org/project/lazyimp/ that I use fairly heavily. Syntactically it's just wrapping otherwise unmodified import statements in a with block, so tools 'just work' and it can be easily disabled or told to import eagerly for debugging. It's powered primarily by swapping out the frame's f_builtins in a cext (as it needs more power than importlib hooks provide), but has a lame attempt at a threadsafe pure python version, and a super dumb global hook version.

I was skeptical and cautious with it at first but I've since moved large chunks of my codebase to it - it's caused surprisingly few problems (honestly none besides forgetting to handle some import-time registration in some modules) and the speed boost is addictive.

Post reply on HN