r/Python • Pythonista • 20d ago

Discussion Mysterious Lazy Imports

Run this on Python 3.15:

>>> lazy import json
>>> type(globals()['json'])
<class 'lazy_import'>

Now try separating that into an innocent variable:

>>> lazy import json
>>> x = globals()['json']
>>> type(x)
<class 'module'>

Looks like the same code, but what?! The output changes! Can you guess why?

(Originally asked as a Stack Overflow question, now posting here as a fun fact 😄)

63 Upvotes

17 comments sorted by

83

u/toxic_acro 20d ago

From the text of PEP 810 (which added lazy imports)

Accessing a lazy object (from a global variable or a module attribute) reifies the object.

However, calling globals() or accessing a module’s __dict__ does not trigger reification – they return the module’s dictionary, and accessing lazy objects through that dictionary still returns lazy proxy objects that need to be manually reified upon use.

Reification (i.e. replacing the lazy import proxy with the actual object) is triggered whenever the object is looked up by name, as you've done here by assigning and then using the variable x

5

u/__peg_parser__ Pythonista 20d ago

Hi, great explanation. Is there a way to store a reference to the LazyImportType object without having it reify when used? I know I could just call globals()['json'] every time, but I'm looking for a way to store it in a variable.

6

u/toxic_acro 20d ago

The trigger for reifying is whenever it is loaded from a "name"

If you want to use the underlying lazy import proxy, you need to make sure that it is never referenced by any name. As you've seen, saving it to a variable and then using that variable is enough to reify the import. You would have to only ever use dictionary lookups (e.g. using globals without ever assigning and using a variable).

I'd question what you're trying to do and why. If you are just trying to poke around at the implementation and understand it better for yourself, that's fine and very reasonable, but if this is for anything for "production" it's a bad idea to be mucking around in the internals

3

u/__peg_parser__ Pythonista 20d ago

Thanks for the reply. I'm just playing around with this new feature and seeing how lazy imports can be introspected.

1

u/raks1991 16d ago

Function locals are the escape hatch here. LOAD_FAST doesn't reify lazy objects (the 3.15 source has no lazy-import check there), so inside a function the proxy survives being bound to a name:

lazy import json

def peek():
    p = globals()["json"]
    return type(p), p.resolve()

p is a fast local, so type(p) should still see the lazy object and p.resolve() reifies on demand. Binding it to a module-level or class-body name puts you back on LOAD_NAME/LOAD_GLOBAL.

1

u/assumptionkrebs1990 16d ago

How can I access __dict__ without triggering the load? I tried both json.__dict__ and dir(json) and after that the module was loaded.

33

u/PwAlreadyTaken 20d ago

I feel like that’s intuitive; one is peeking inside the module box, the other is taking something out and putting it in a variable.

7

u/Ducksual 20d ago

Note that:

>>> lazy import json
>>> x = globals()['json']
>>> globals()['x']
<lazy_import 'json'>
>>> x
<module 'json' from '...json/__init__.py'>
>>> globals()['x']
<module 'json' from '...json/__init__.py'>
>>> globals()['json']
<lazy_import 'json'>

The import is only resolved and replaced when it's loaded from globals. When you get globals as a dict it doesn't resolve the import. x = globals()['json'] assigns the lazy import object to x.

Calling type(x) looks up x in globals, x is a lazy import object so it gets resolved.

2

u/VenkatKnows 10d ago

Name lookup reifies lazy objects, a dict subscript doesn't. So x holds the lazy proxy, but the moment you load x by name it resolves to the real module. Fun until it bites you in prod.

1

u/DigThatData 20d ago

didn't even realize this was a thing, guess I missed the 3.15 release

0

u/unicornshedsocial 20d ago

The difference is the load: type(x) looks x up by name (LOAD_NAME at the REPL), which reifies the proxy, while globals()[...] hands you the raw dict value. Did the Stack Overflow answer explain it that way?

-2

u/ShelLuser42 It works on my machine 20d ago edited 20d ago

My guess is the lazy import even though I never used it before.

My assumption, based on the output, is that it adds an "interface" which fact checks if the imported object gets addressed, and if so that routine takes care of doing the actual import check(s) afterwards.

(edit)

So what I'm saying: assigning it to a variable triggers that "interface", thus leaving you with the end result.

-6

u/Rudeus_Kino 20d ago

In python you can override assignment, just write get_.

-8

u/ohnoitssobig 20d ago

Trivially answered by looking at the bytecode.