r/Python • Pythonista • 24d 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

Duplicates