on-internet
← Community

Dynamic equivalent to the import statement in Python 3.15+

Stack Overflow ·

importlib.import_module() (and the discouraged builltins.__import__() / importlib.__import__) allow dynamically importing a module. However, it has one important difference from the import statement: it doesn't respect lazy filters (a feature in Python 3.15+). For example, let's make all imports lazy: import sys make_normal_imports_also_lazy = lambda importing, imported, fromlist: True sys.set_lazy_imports('all') sys.set_lazy_imports_filter(make_normal_imports_also_lazy) Now if I use the import stmt, I get a lazy import: import json print(type(globals()['json'])) # <class 'lazy_import'> But if

Comments

Sign in or sign up to comment and upvote.

No comments yet.