Python 3.15+sysfunction
sys.set_lazy_imports()
modules
Switches the lazy-import mode of the whole application: 'normal' makes only imports marked lazy lazy, 'all' makes every top-level import lazy.
Syntax
sys.set_lazy_imports(mode)
This is a lever for the application owner, not for a library: the mode is global and changes the behaviour of other people's code, so a package others import should not call it. 'all' makes every module-level import in modules loaded after the call potentially lazy; modules already loaded do not change. The same mode can be set without touching code: python -X lazy_imports=all or the PYTHON_LAZY_IMPORTS environment variable. Fine-grained control over what exactly is lazy comes from the filter, sys.set_lazy_imports_filter.
Examples
import sys
print(sys.get_lazy_imports()) # → normal
sys.set_lazy_imports('all')
print(sys.get_lazy_imports()) # → all
sys.set_lazy_imports('normal')
See also
Open in the full glossarydocs.python.orgReport an inaccuracy