

Join the discussion
Write your take first — we'll ask for email only when you're ready to publish.
- Hacker News
- 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.
- You have always been able to do the same thing in Python. This PEP isn't needed for that functionality.by markrages
- > 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 :)
- 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.
by ta371jashgG - Next we need
lazy import *by ant6n - Is that not the purpose of the global switch outlined in the PEP?by swiftcoder
- I think HN is translating the link somehow? It should be directing to this post: https://discuss.python.org/t/pep-810-explicit-lazy-imports/1...
"""
Dear PEP 810 authors. The Steering Council is happy to unanimously [4 votes, as Pablo cannot vote] accept “PEP 810, Explicit lazy imports”. Congratulations! We appreciate the way you were able to build on and improve the previously discussed (and rejected) attempt at lazy imports as proposed in PEP 690.
We have recommendations about some of the PEP’s details, a few suggestions for filling a couple of small gaps, and we have made decisions on the alternatives that you’ve left to the SC, all of which I’ll outline below. If you have any questions, please do reach out to the SC for clarification, either here, on the SC tracker, or in office hours.
Use lazy as the keyword. We debated many of the given alternatives (and some we came up with ourselves), and ultimately agreed with the PEP’s choice of the lazy keyword. The closest challenger was defer, but once we tried to use that in all the places where the term is visible, we ultimately didn’t think it was as good an overall fit. The same was true with all the other alternative keywords we could come up with, so… lazy it is!
What about from foo lazy import bar? Nope! We like that in both module imports and from-imports that the lazy keyword is the first thing on the line. It helps to visually recognize lazy imports of both varieties.
Leveraging a subclass of dict. We don’t see a need for this complicated alternative; please add this to the rejected ideas.
Allowing ’*’ in __lazy_modules__. We agree with the rationale for rejecting this idea; it can always be added later if needed.
One thing that the PEP does not mention is .pth files, which the site.py module processes, and which has some special handling for lines that begin with the string 'import' followed by a space or tab. It doesn’t make much sense for .pth files to support lazy imports, so we suggest that the PEP explicitly says that this special handling in .pth files will not be adapted to handle lazy imports.
There currently is no way to get the active filter mode, so please add a sys.get_lazy_imports() function. Also, do you think appending _mode to their names makes the purpose of these functions clearer? We leave that up to the PEP authors.
The PEP should be explicit about the precedence order between the different ways to set the mode, i.e. $PYTHON_LAZY_IMPORTS=<mode>, -X lazy_imports=<mode>, and sys.set_lazy_imports(). In all expectation, it will follow the same precedence order as other similar settings, but the PEP should be explicit.
We agree that the PEP should take no position on any style recommendations for sorting lazy imports. While we generally like the idea of grouping lazy imports together, let’s leave that up to the linters and auto-formatters to decide the details.
That should just about cover it. Again, thank you for your work on this, as it’s been a feature so many in the Python community have wanted for so long. Given the earlier attempts and existing workarounds, we think this strikes exactly the right balance.
-Barry, on behalf of the Python Steering Council
"""
by Redoubts - HN is using the canonical URL for the page. See https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes...by lolpython
- Some folks at HRT[0] will probably be unhappy about that lol
0.https://www.hudsonrivertrading.com/hrtbeat/inside-hrts-pytho...
by curiousgal - Why would they? The accepted PEP just has extra keywords to mark imports explicit. That's a source level find-and-replace. They already did most of the real work in their codebase, such as finding where the code depended on the side effect of importing a module.by kccqzy
- I'm confused, wouldn't HRT be happy about this? The article you linked specifically states "..we hope to propose a revised lazy imports PEP that introduces an explicit lazy keyword.."
Is that not exactly what PEP 810 proposes?
by seemaze - Look at the names in the PEP [1], this PEP is written by them
- Given you can already trigger import in a local scope, it's merely a syntax sugar though.by mmis1000
- This will be huge at the place I work!
I’m unfamiliar with the PEP process. How long until this makes it into a Python version?
by johnfn - It should land in 3.15, so October next year. https://peps.python.org/pep-0790/by joerick
- This one is scheduled to land in the next "minor" version, 3.15. Python has an annual release cadence; 3.14 came out recently and 3.15 is due next October.
In general, most PEPs are authored targeting the "next minor version" at the time of proposal; but they may be intentionally deferred at the start, and sometimes the process can take multiple years anyway.
There are also PEPs that don't involve any change to the Python language, standard library or interpreter. In particular, there are PEPs that exist simply to document existing practice (or changes thereto), PEPs that concern governance (the Python Software Foundation, the Steering Council etc.), and PEPs that cover related special interests, such as packaging standards (which in turn can range from technical details about how metadata is formatted, to changes in PyPI's API).
by zahlman - Source phase imports are stage 3 (recommended for implementation, no major updates expected) in JS. "Import source" tells the runtime to go get the code, but not yet run it. Similar ideas seemingly to what's going on here in python! https://github.com/tc39/proposal-source-phase-imports
- This proposal defers even looking for the code. The LazyLoader already in the standard library would eagerly look for code, but just record a file path and not actually store any bytecode data, never mind deserializing or running it.
The rationale described in the PEP is that some systems try to `import`, for example, across a network share, so even searching the filesystem is slow and there is a desire to defer that (and avoid it on runs where the corresponding code isn't executed).
by zahlman - That was fast. It was sent to SC 20 days ago. (Not complaining. I am happy with the outcome).by ayhanfuat
- Quite a few PEPs are accepted this quickly, actually.
But some others take literally years.
by zahlman - Source link indicating PEP 810 was accepted:
https://discuss.python.org/t/pep-810-explicit-lazy-imports/1...
by alberth - Python is quickly turning into a crowded keyword junkyardby nothrowaways
- > Python is quickly turning into a crowded keyword junkyard
* Javascript (ECMAScript) has 63 keywords. * Rust has 50 keywords. * Java has 51 keywords + 17 contextually reserved words, for a total of 68. * Python has now 36 keywords + 4 'soft' keywords, for a total of 40. * Go has 25 keywords.
by aroberge - It is a 'soft keyword' as the PEP explains. I would not think that this has any major impact on anyone who just chooses to ignore this feature. Assuming that you want this behavior, I wonder how this could have been done in a better fashion without now having 'lazy' in the specific context of an import statement.by riedel
- The pep didn’t mention considering reusing `async` instead of `lazy`. That would’ve conveyed the same thing to me without a new keyword, and would haven’t been similar to html’s usage `async`.by onedognight
- From the PEP (https://peps.python.org/pep-0810/):
> The choice to introduce a new `lazy` keyword reflects the need for explicit syntax. Lazy imports have different semantics from normal imports: errors and side effects occur at first use rather than at the import statement. This semantic difference makes it critical that laziness is visible at the import site itself, not hidden in global configuration or distant module-level declarations. The lazy keyword provides local reasoning about import behavior, avoiding the need to search elsewhere in the code to understand whether an import is deferred. The rest of the import semantics remain unchanged: the same import machinery, module finding, and loading mechanisms are used.
This functionality is highly desired, and it does appear to actually need a new (soft) keyword. Sorry you don't like it.
by striking - Python has about 40 keywords, I say I would regularly use about 30, and irregularly use about another 5. Hardly seems like a "junkyard".
Further, this lack of first class support for lazy importing has spawned multiple CPython forks that implement their own lazy importing or a modified version of the prior rejected PEP 690. Reducing the real world need for forks seems worth the price of one keyword.
by notatallshaw - How will lazy imports interact with PEP 8 which recommends grouping imports in order:
1. Standard library imports.
2. Related third party imports.
3. Local application/library specific imports.
https://peps.python.org/pep-0008/#imports
Based on the examples in PEP 810, I suppose each group of regular imports can be followed by a group of lazy imports?
import os import sys lazy import json import fastapi lazy import numpy import myapi import mymodels lazy import myutils ...by divbzero