I suspect this change in Python will dramatically improve the performance of such large programs as well.
You have to create wrappers in languages like JavaScript, Lua, Python, etc. to create the same behavior.
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 :)
m = importlib.import_module(requests.get("http://localhost:8000/package_name").content.strip().decode("ASCII"))
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.
But some others take literally years.
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).
I’m unfamiliar with the PEP process. How long until this makes it into a Python version?
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).
Regardless lazy loading needs widespread use to be most effective so having a unified syntax and no extra dependencies makes a lot of sense.
0.https://www.hudsonrivertrading.com/hrtbeat/inside-hrts-pytho...
Is that not exactly what PEP 810 proposes?
> 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...
https://discuss.python.org/t/pep-810-explicit-lazy-imports/1...
    lazy import *  Where <mode> can be:
  
      "normal" (or unset): Only explicitly marked lazy imports are lazy
      "all": All module-level imports (except in try blocks and import *) become   potentially lazy
      "none": No imports are lazy, even those explicitly marked with lazy keyword
  When the global flag is set to "all", all imports at the global level of all modules are potentially lazy except for those inside a try block or any wild card (from ... import *) import."A new soft keyword lazy is added. A soft keyword is a context-sensitive keyword that only has special meaning in specific grammatical contexts; elsewhere it can be used as a regular identifier (e.g., as a variable name). The lazy keyword only has special meaning when it appears before import statements..."
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.
Hard Keywords:
False await else import pass None break except in raise True class finally is return and continue for lambda try as def from nonlocal while assert del global not with async elif if or yield
Soft Keywords:
match case _ type
I think nonlocal/global are the only hard keywords I now barely use, for the soft ones I rarely use pattern matching, so 5 seems like a good estimate
> 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.
Redoubts•6h ago
"""
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
"""
lolpython•6h ago
NooneAtAll3•2h ago
lolpython•1h ago