[Python-Dev] More optimisation ideas

Antoine Pitrou antoine at python.org
Sun Jan 31 12:09:49 EST 2016


Brett Cannon <brett <at> python.org> writes:

> 
> 
> A lazy importer was added in Python 3.5 and it was not possible
> without the module spec refactoring.

Wow... Thank you, I didn't know about that.

Now for the next question: how am I supposed to use it?

The following documentation leaves me absolutely clueless:

"""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). 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."""

(reference:
https://docs.python.org/3/library/importlib.html#importlib.util.LazyLoader)

I want to import lazily the modules from package "foobar.*", but not
other modules as other libraries may depend on import side effects.
How do I do that? The quoted snippet doesn't really help.

Regards

Antoine.


More information about the Python-Dev mailing list