importlib.abc.Loader

class importlib.abc.Loader

An abstract base class for a loader. See PEP 302 for the exact definition for a loader.

create_module(spec)

A method that returns the module object to use when importing a module. This method may return None, indicating that default module creation semantics should take place.

New in version 3.4.

Changed in version 3.5: Starting in Python 3.6, this method will not be optional when exec_module() is defined.

exec_module(module)

An abstract method that executes the module in its own namespace when a module is imported or reloaded. The module should already be initialized when exec_module() is called.

New in version 3.4.

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.

module_repr(module)

A legacy method which when implemented calculates and returns the given module’s repr, as a string. The module type’s default repr() will use the result of this method as appropriate.

New in version 3.3.

Changed in version 3.4: Made optional instead of an abstractmethod.

Deprecated since version 3.4: The import machinery now takes care of this automatically.

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

Please login to continue.