Python 3.15+sysfunction
sys.set_lazy_imports_filter()
modules
Installs a filter that decides, for each import, whether it may be lazy. The filter receives module names and returns True or False; None removes it.
Syntax
sys.set_lazy_imports_filter(filter)
The filter is called with three positional arguments: the name of the importing module, the full name of the imported one (for lazy from .spam import eggs, package.spam) and fromlist, a tuple of names for a from-import or None. True allows laziness, False forces an immediate load. A typical use is the 'all' mode for your own modules only: the filter passes names starting with 'myapp.' while third-party dependencies load as usual. It is a tool for narrow cases; an ordinary program is fine with the lazy keyword.
Examples
import sys
def only_mine(importing, imported, fromlist):
return imported.startswith('myapp.')
sys.set_lazy_imports_filter(only_mine)
print(sys.get_lazy_imports_filter() is only_mine) # → True
sys.set_lazy_imports_filter(None)
print(sys.get_lazy_imports_filter()) # → None
See also
Open in the full glossarydocs.python.orgReport an inaccuracy