importlib.abc.Loader.load_module()

load_module(fullname)

A legacy method for loading a module. If the module cannot be loaded, ImportError is raised, otherwise the loaded module is returned.

If the requested module already exists in sys.modules, that module should be used and reloaded. Otherwise the loader should create a new module and insert it into sys.modules before any loading begins, to prevent recursion from the import. If the loader inserted a module and the load fails, it must be removed by the loader from sys.modules; modules already in sys.modules before the loader began execution should be left alone (see importlib.util.module_for_loader()).

The loader should set several attributes on the module. (Note that some of these attributes can change when a module is reloaded):

  • __name__

    The name of the module.

  • __file__

    The path to where the module data is stored (not set for built-in modules).

  • __cached__

    The path to where a compiled version of the module is/should be stored (not set when the attribute would be inappropriate).

  • __path__

    A list of strings specifying the search path within a package. This attribute is not set on modules.

  • __package__

    The parent package for the module/package. If the module is top-level then it has a value of the empty string. The importlib.util.module_for_loader() decorator can handle the details for __package__.

  • __loader__

    The loader used to load the module. The importlib.util.module_for_loader() decorator can handle the details for __package__.

When exec_module() is available then backwards-compatible functionality is provided.

Changed in version 3.4: Raise ImportError when called instead of NotImplementedError. Functionality provided when exec_module() is available.

Deprecated since version 3.4: The recommended API for loading a module is exec_module() (and create_module()). Loaders should implement it instead of load_module(). The import machinery takes care of all the other responsibilities of load_module() when exec_module() is implemented.

doc_python
2016-10-07 17:34:34
Comments
Leave a Comment

Please login to continue.