@importlib.util.module_for_loader
A decorator for importlib.abc.Loader.load_module()
to handle selecting the proper module object to load with. The decorated method is expected to have a call signature taking two positional arguments (e.g. load_module(self, module)
) for which the second argument will be the module object to be used by the loader. Note that the decorator will not work on static methods because of the assumption of two arguments.
The decorated method will take in the name of the module to be loaded as expected for a loader. If the module is not found in sys.modules
then a new one is constructed. Regardless of where the module came from, __loader__
set to self and __package__
is set based on what importlib.abc.InspectLoader.is_package()
returns (if available). These attributes are set unconditionally to support reloading.
If an exception is raised by the decorated method and a module was added to sys.modules
, then the module will be removed to prevent a partially initialized module from being in left in sys.modules
. If the module was already in sys.modules
then it is left alone.
Changed in version 3.3: __loader__
and __package__
are automatically set (when possible).
Changed in version 3.4: Set __name__
, __loader__
__package__
unconditionally to support reloading.
Deprecated since version 3.4: The import machinery now directly performs all the functionality provided by this function.
Please login to continue.