Top
Best
New

Posted by Redoubts 11/3/2025

Python Steering Council unanimously accepts "PEP 810, Explicit lazy imports"(discuss.python.org)
170 points | 61 commentspage 2
ta371jashgG 11/4/2025|
This has been rejected several times before. Now that Google and Microsoft fired core developers, Python Inc. is owned by Meta. Meta supports lazy imports in Cinder and needs it for its atrocious and bloated scientific ecosystem so that the bloat loads in less than 5 min.

If Meta also starts firing Python developers, development might return to normal.

andrewmcwatters 11/3/2025||
I've noticed that instead of defining requires at the top of Lua files, if you can know your own program well enough, defining them right before the dependency is actually used makes large Lua programs much, much quicker. Generally speaking, startup times can be a sped up by a meaningful factor.

I suspect this change in Python will dramatically improve the performance of such large programs as well.

embedding-shape 11/3/2025||
> I suspect this change in Python will dramatically improve the performance of such large programs as well.

Makes packaging super fun too, where you need to hit every possible path so you don't miss anything imported in 1% of the execution paths :)

KptMarchewa 11/3/2025|||
I can't even express how negatively I feel about build/packaging systems that process dependencies based on code-level imports instead of some explicit build manifest separate to the code.
embedding-shape 11/3/2025|||
You and me both, but life as a consultant/freelancer requires you to drag yourself through dirt sometimes to make it out on the other side.
jacquesm 11/3/2025|||
Not to mention the potential for runtime errors long after the code has started up.
dragonwriter 11/3/2025||||
Or, you could just use a project-level specification file to list dependencies rather than looking for imports in the code, trying to figure out what they resolve to, and trying to package the results.
snovv_crash 11/3/2025|||
Can't you do some kind of static analysis instead?
Figs 11/3/2025|||
Depends if your code has horrors like this lurking in it:

m = importlib.import_module(requests.get("http://localhost:8000/package_name").content.strip().decode("ASCII"))

falcor84 11/3/2025|||
If you want even better nightmares, you can make localhost:8000 forward to a container running claude code with --dangerously-skip-permissions which uses an unkindness of mcp servers to control that endpoint on the fly based (amongst other sources) on 4chan's /b/.
zahlman 11/3/2025|||
> which uses an unkindness of mcp servers

I guess you meant "a feature of MCP servers which is unkind", but I couldn't help but interpret "unkindness" as the collective noun for a group of MCP servers.

falcor84 11/3/2025||
That was indeed the intent. I first considered "a conspiracy" like with lemurs, but eventually felt that "unkindness" was more appropriate.
snovv_crash 11/3/2025|||
Better to let the viewers on a twitch stream vote for it.
im3w1l 11/3/2025|||
Since this should be a rare thing I don't think it's unreasonable to require users of patterns like this to put some kind of special annotation for that static analysis tool saying "it may not look like it but I'm doing an import here".
fithisux 11/3/2025|||
Yes. It is much more clear to be explicit though.
jacquesm 11/3/2025||
Safer too.
markrages 11/3/2025||
You have always been able to do the same thing in Python. This PEP isn't needed for that functionality.
andrewmcwatters 11/3/2025|||
Yeah, I'm aware the same behavior is available, but this proposal creates a call trigger on the dependency which requires far less analysis on larger projects to understand where the import needs to be moved to.

You have to create wrappers in languages like JavaScript, Lua, Python, etc. to create the same behavior.

skywhopper 11/3/2025|||
You can declare imports at the beginning of a program that don’t load until they are used?
markrages 11/4/2025|||
"defining them right before the dependency is actually used"
xenator 11/3/2025||
Does it conform Occam's razor rule to have something that can be easily done very similar way without changing language?
boothby 11/3/2025||
Having some limited experience with lazy imports, yes, but this eliminates a lot of gross boilerplate. It also has the effect of "blessing" the practice of lazy imports which can have a cultural impact; it also prevents a situation wherein multiple subtly incompatible approaches to lazy imports become individually popular.
contravariant 11/3/2025||
Not sure that's Occam's razor any more.

Regardless lazy loading needs widespread use to be most effective so having a unified syntax and no extra dependencies makes a lot of sense.

manbitesdog 11/3/2025|
This is great for building modules. One can now lazy import all interesting names on __init__.py, so that instead of having to remember `from module.some_submodule_that_you_need_to_remember import method` you can just do `from module import name`.
maxbond 11/3/2025|
https://peps.python.org/pep-0810/#what-about-star-imports-fr...

> What about star imports (`from module import *`)?

> Wild card (star) imports cannot be lazy - they remain eager. This is because the set of names being imported cannot be determined without loading the module. Using the lazy keyword with star imports will be a syntax error. If lazy imports are globally enabled, star imports will still be eager.

Additionally, star imports can interfere with type checkers and IDEs and shadowing caused by star imports is a frequent and difficult to diagnose source of bugs (you analyze the function you think you're calling and find no issues, but you're actually calling a different function because the star import occurs after your explicit import).

You might be able to workaround this limitation by doing a lazy import into an intermediate module (a prelude) on a name by name basis and then star import that intermediate module. But personally I solve this problem using IDE features.

https://github.com/python-lsp/python-lsp-server/blob/develop...

manbitesdog 11/5/2025||
? This has nothing to do with star imports
maxbond 11/5/2025||
My bad for misreading you