importlib.util.resolve_name()

importlib.util.resolve_name(name, package) Resolve a relative module name to an absolute one. If name has no leading dots, then name is simply returned. This allows for usage such as importlib.util.resolve_name('sys', __package__) without doing a check to see if the package argument is needed. ValueError is raised if name is a relative module name but package is a false value (e.g. None or the empty string). ValueError is also raised a relative name would escape its containing package (e.g.

importlib.util.module_from_spec()

importlib.util.module_from_spec(spec) Create a new module based on spec and spec.loader.create_module(). If spec.loader.create_module() does not return None, then any pre-existing attributes will not be reset. Also, no AttributeError will be raised if triggered while accessing spec or setting an attribute on the module. This function is preferred over using types.ModuleType to create a new module as spec is used to set as many import-controlled attributes on the module as possible. New in v

importlib.util.module_for_loader()

@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 t

importlib.util.MAGIC_NUMBER

importlib.util.MAGIC_NUMBER The bytes which represent the bytecode version number. If you need help with loading/writing bytecode then consider importlib.abc.SourceLoader. New in version 3.4.

importlib.util.LazyLoader.factory()

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

importlib.util.LazyLoader

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

importlib.util.find_spec()

importlib.util.find_spec(name, package=None) Find the spec for a module, optionally relative to the specified package name. If the module is in sys.modules, then sys.modules[name].__spec__ is returned (unless the spec would be None or is not set, in which case ValueError is raised). Otherwise a search using sys.meta_path is done. None is returned if no spec is found. If name is for a submodule (contains a dot), the parent module is automatically imported. name and package work the same as fo

importlib.util.decode_source()

importlib.util.decode_source(source_bytes) Decode the given bytes representing source code and return it as a string with universal newlines (as required by importlib.abc.InspectLoader.get_source()). New in version 3.4.

importlib.util.cache_from_source()

importlib.util.cache_from_source(path, debug_override=None, *, optimization=None) Return the PEP 3147/PEP 488 path to the byte-compiled file associated with the source path. For example, if path is /foo/bar/baz.py the return value would be /foo/bar/__pycache__/baz.cpython-32.pyc for Python 3.2. The cpython-32 string comes from the current magic tag (see get_tag(); if sys.implementation.cache_tag is not defined then NotImplementedError will be raised). The optimization parameter is used to sp

importlib.reload()

importlib.reload(module) Reload a previously imported module. The argument must be a module object, so it must have been successfully imported before. This is useful if you have edited the module source file using an external editor and want to try out the new version without leaving the Python interpreter. The return value is the module object (which can be different if re-importing causes a different object to be placed in sys.modules). When reload() is executed: Python module’s code is re