You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The output changes depending on True / False in the filter, which is intended. It also works if I switch out lazy import json for json = __lazy_import__('json').
Unless this was documented differently I believe this is the correct behavior though. __import__ is a low-level primitive you are not meant to call directly and it would make more sense to me that it remains as is.
Rationale why I believe so: in some cases, you might want to have a primitive that ignores any existing filters and bypasses the "all" lazy mode in case you need an eager import, and i believe __import__ should do that. As for __lazy_import__, I believe it should return either a real or true module module since it doesn't know whether it was already imported I guess (and we also removed the "no lazy import" mode I think).
OTOH, if it's easier to bring the code to parity, we can consider it. But that could break existing projects that relied on the fact that __import__ is unconditinally eager. I haven't read the PEP in its entirety but I'd like to know first if the original specs considered this or not.
__import__ bypasses lazy filters; how can I respect lazy filters? There's no __dynamically_import_while_respecting_lazy__().
__import__ bypasses lazy filters but __lazy_import__ doesn't. How can this inconsistency be explained? What if I want __lazy_import__ to bypass lazy filters too?
As I said, if the PEP does not specify the behavior of __import__ it might be tricky to change it. This is why I asked whether there was something about it and why I asked the PEP authors first.
Regular import and from ... import ... statements remain eager unless explicitly made potentially lazy by the local or global mechanisms provided.
Dynamic import APIs remain eager and unchanged: __import__() and `importlib.import_module().
Activity
picnixz commentedon Nov 10, 2026
Unless this was documented differently I believe this is the correct behavior though.
__import__is a low-level primitive you are not meant to call directly and it would make more sense to me that it remains as is.cc @DinoV @pablogsal
picnixz commentedon Nov 11, 2026
Rationale why I believe so: in some cases, you might want to have a primitive that ignores any existing filters and bypasses the "all" lazy mode in case you need an eager import, and i believe
__import__should do that. As for__lazy_import__, I believe it should return either a real or true module module since it doesn't know whether it was already imported I guess (and we also removed the "no lazy import" mode I think).OTOH, if it's easier to bring the code to parity, we can consider it. But that could break existing projects that relied on the fact that
__import__is unconditinally eager. I haven't read the PEP in its entirety but I'd like to know first if the original specs considered this or not.JadenV15 commentedon Nov 12, 2026
@picnixz
__import__bypasses lazy filters; how can I respect lazy filters? There's no__dynamically_import_while_respecting_lazy__().__import__bypasses lazy filters but__lazy_import__doesn't. How can this inconsistency be explained? What if I want__lazy_import__to bypass lazy filters too?[-]__import__ too greedy, make it lazy[/-][+]__import__ too eager, make it lazy[/+]picnixz commentedon Nov 12, 2026
As I said, if the PEP does not specify the behavior of
__import__it might be tricky to change it. This is why I asked whether there was something about it and why I asked the PEP authors first.JadenV15 commentedon Nov 12, 2026
Edited to include examples from
importlibpicnixz commentedon Nov 12, 2026
I looked at the PEP and https://peps.python.org/pep-0810/#unchanged-semantics says:
So this is working as intended.
[-]__import__ too eager, make it lazy[/-][+]`__import__` too eager, make it lazy[/+]