Back to module index
Go to module by name
importlib
importlib.machinery
The machinery of importlib: finders, loaders, hooks, etc.
Classes
BuiltinImporter
Meta path import for built-in modules.
All methods are either class or static methods to avoid the need to
instantiate the class.
create_module(spec)
Create a built-in module
exec_module(module)
Exec a built-in module
find_module(fullname, path=None)
Find the built-in module.
If 'path' is ever specified then the search is considered a failure.
This method is deprecated. Use find_spec() instead.
find_spec(fullname, path=None, target=None)
get_code(fullname)
Return None as built-in modules do not have code objects.
get_source(fullname)
Return None as built-in modules do not have source code.
is_package(fullname)
Return False as built-in modules are never packages.
_load_module_shim(fullname)
Load the specified module into sys.modules and return it.
This method is deprecated. Use loader.exec_module instead.
module_repr(module)
Return repr for the module.
The method is deprecated. The import machinery does the job itself.
ExtensionFileLoader
Loader for extension modules.
The constructor is designed to work with FileFinder.
contents(self)
create_module(self, spec)
Create an unitialized extension module
exec_module(self, module)
Initialize an extension module
get_code(self, fullname)
Return None as an extension module cannot create a code object.
get_data(self, path)
Return the data from path as raw bytes.
get_filename(self, name=None, *args, **kwargs)
Return the path to the source file as found by the finder.
get_resource_reader(self, name=None, *args, **kwargs)
get_source(self, fullname)
Return None as extension modules have no source code.
is_package(self, fullname)
Return True if the extension module is a package.
is_resource(self, name)
load_module(self, name=None, *args, **kwargs)
Load a module from a file.
This method is deprecated. Use exec_module() instead.
open_resource(self, resource)
resource_path(self, resource)
FileFinder
File-based finder.
Interactions with the file system are cached for performance, being
refreshed when the directory the finder is handling has been modified.
find_loader(self, fullname)
Try to find a loader for the specified module, or the namespace
package portions. Returns (loader, list-of-portions).
This method is deprecated. Use find_spec() instead.
_find_module_shim(self, fullname)
Try to find a loader for the specified module by delegating to
self.find_loader().
This method is deprecated in favor of finder.find_spec().
find_spec(self, fullname, target=None)
Try to find a spec for the specified module.
Returns the matching spec, or None if not found.
invalidate_caches(self)
Invalidate the directory mtime.
path_hook(*loader_details)
A class method which returns a closure to use on sys.path_hook
which will return an instance using the specified loaders and the path
called on the closure.
If the path called on the closure is not a directory, ImportError is
raised.
FrozenImporter
Meta path import for frozen modules.
All methods are either class or static methods to avoid the need to
instantiate the class.
create_module(spec)
Use default semantics for module creation.
exec_module(module)
find_module(fullname, path=None)
Find a frozen module.
This method is deprecated. Use find_spec() instead.
find_spec(fullname, path=None, target=None)
get_code(fullname)
Return the code object for the frozen module.
get_source(fullname)
Return None as frozen modules do not have source code.
is_package(fullname)
Return True if the frozen module is a package.
load_module(fullname)
Load a frozen module.
This method is deprecated. Use exec_module() instead.
module_repr(m)
Return repr for the module.
The method is deprecated. The import machinery does the job itself.
ModuleSpec
The specification for a module, used for loading.
A module's spec is the source for information about the module. For
data associated with the module, including source, use the spec's
loader.
`name` is the absolute name of the module. `loader` is the loader
to use when loading the module. `parent` is the name of the
package the module is in. The parent is derived from the name.
`is_package` determines if the module is considered a package or
not. On modules this is reflected by the `__path__` attribute.
`origin` is the specific location used by the loader from which to
load the module, if that information is available. When filename is
set, origin will match.
`has_location` indicates that a spec's "origin" reflects a location.
When this is True, `__file__` attribute of the module is set.
`cached` is the location of the cached bytecode file, if any. It
corresponds to the `__cached__` attribute.
`submodule_search_locations` is the sequence of path entries to
search when importing submodules. If set, is_package should be
True--and False otherwise.
Packages are simply modules that (may) have submodules. If a spec
has a non-None value in `submodule_search_locations`, the import
system will consider modules loaded from the spec as packages.
Only finders (see importlib.abc.MetaPathFinder and
importlib.abc.PathEntryFinder) should modify ModuleSpec instances.
cached = <property object at 0x7f0227c5c680>
has_location = <property object at 0x7f0227c5c720>
parent = <property object at 0x7f0227c5c590>
The name of the module's parent.
PathFinder
Meta path finder for sys.path and package __path__ attributes.
find_distributions(*args, **kwargs)
Find distributions.
Return an iterable of all Distribution instances capable of
loading the metadata for packages matching ``context.name``
(or all names if ``None`` indicated) along the paths in the list
of directories ``context.path``.
find_module(fullname, path=None)
find the module on sys.path or 'path' based on sys.path_hooks and
sys.path_importer_cache.
This method is deprecated. Use find_spec() instead.
find_spec(fullname, path=None, target=None)
Try to find a spec for 'fullname' on sys.path or 'path'.
The search is based on sys.path_hooks and sys.path_importer_cache.
invalidate_caches()
Call the invalidate_caches() method on all path entry finders
stored in sys.path_importer_caches (where implemented).
SourceFileLoader
Concrete implementation of SourceLoader using the file system.
contents(self)
create_module(self, spec)
Use default semantics for module creation.
exec_module(self, module)
Execute the module.
get_code(self, fullname)
Concrete implementation of InspectLoader.get_code.
Reading of bytecode requires path_stats to be implemented. To write
bytecode, set_data must also be implemented.
get_data(self, path)
Return the data from path as raw bytes.
get_filename(self, name=None, *args, **kwargs)
Return the path to the source file as found by the finder.
get_resource_reader(self, name=None, *args, **kwargs)
get_source(self, fullname)
Concrete implementation of InspectLoader.get_source.
is_package(self, fullname)
Concrete implementation of InspectLoader.is_package by checking if
the path returned by get_filename has a filename of '__init__.py'.
is_resource(self, name)
load_module(self, name=None, *args, **kwargs)
Load a module from a file.
This method is deprecated. Use exec_module() instead.
open_resource(self, resource)
path_mtime(self, path)
Optional method that returns the modification time (an int) for the
specified path (a str).
Raises OSError when the path cannot be handled.
path_stats(self, path)
Return the metadata for the path.
resource_path(self, resource)
set_data(self, path, data, *, _mode=438)
Write bytes data to a file.
source_to_code(self, data, path, *, _optimize=-1)
Return the code object compiled from source.
The 'data' argument can be any object type that compile() supports.
SourcelessFileLoader
Loader which handles sourceless file imports.
contents(self)
create_module(self, spec)
Use default semantics for module creation.
exec_module(self, module)
Execute the module.
get_code(self, fullname)
get_data(self, path)
Return the data from path as raw bytes.
get_filename(self, name=None, *args, **kwargs)
Return the path to the source file as found by the finder.
get_resource_reader(self, name=None, *args, **kwargs)
get_source(self, fullname)
Return None as there is no source code.
is_package(self, fullname)
Concrete implementation of InspectLoader.is_package by checking if
the path returned by get_filename has a filename of '__init__.py'.
is_resource(self, name)
load_module(self, name=None, *args, **kwargs)
Load a module from a file.
This method is deprecated. Use exec_module() instead.
open_resource(self, resource)
resource_path(self, resource)
WindowsRegistryFinder
Meta path finder for modules declared in the Windows registry.
find_module(fullname, path=None)
Find module named in the registry.
This method is deprecated. Use exec_module() instead.
find_spec(fullname, path=None, target=None)
DEBUG_BUILD = False
REGISTRY_KEY = 'Software\\Python\\PythonCore\\{sys_version}\\Modules\\{fullname}'
REGISTRY_KEY_DEBUG = 'Software\\Python\\PythonCore\\{sys_version}\\Modules\\{fullname}\\Debug'
Functions
all_suffixes
all_suffixes()
Returns a list of all recognized module suffixes for this process
Other members
BYTECODE_SUFFIXES = ['.pyc']
DEBUG_BYTECODE_SUFFIXES = ['.pyc']
EXTENSION_SUFFIXES = ['.cpython-39-x86_64-linux-gnu.so', '.abi3.so', '.so']
OPTIMIZED_BYTECODE_SUFFIXES = ['.pyc']
SOURCE_SUFFIXES = ['.py']