class importlib.util.LazyLoader(loader)
A class which postpones the execution of the loader of a module until the module has an attribute accessed.
This class only works with loaders that define exec_module()
as control over what module type is used for the module is required. For those same reasons, the loader’s create_module()
method will be ignored (i.e., the loader’s method should only return None
; this excludes BuiltinImporter
and ExtensionFileLoader
). Finally, modules which substitute the object placed into sys.modules
will not work as there is no way to properly replace the module references throughout the interpreter safely; ValueError
is raised if such a substitution is detected.
Note
For projects where startup time is critical, this class allows for potentially minimizing the cost of loading a module if it is never used. For projects where startup time is not essential then use of this class is heavily discouraged due to error messages created during loading being postponed and thus occurring out of context.
New in version 3.5.
-
classmethod factory(loader)
-
A static method which returns a callable that creates a lazy loader. This is meant to be used in situations where the loader is passed by class instead of by instance.
suffixes = importlib.machinery.SOURCE_SUFFIXES loader = importlib.machinery.SourceFileLoader lazy_loader = importlib.util.LazyLoader.factory(loader) finder = importlib.machinery.FileFinder(path, (lazy_loader, suffixes))
Please login to continue.