Back to module index
Go to module by name
importlib
importlib.util
Utility code for constructing importers, etc.
Classes
LazyLoader
A loader that creates a module which defers loading until attribute access.
create_module(self, spec)
exec_module(self, module)
Make the module load lazily.
factory(loader)
Construct a callable which returns the eager loader made lazy.
load_module(self, fullname)
Return the loaded module.
The module must be added to sys.modules and have import-related
attributes set properly. The fullname is a str.
ImportError is raised on failure.
This method is deprecated in favor of loader.exec_module(). If
exec_module() exists then it is used to provide a backwards-compatible
functionality for this method.
module_repr(self, module)
Return a module's repr.
Used by the module type when the method does not raise
NotImplementedError.
This method is deprecated.
Functions
cache_from_source
cache_from_source(path, debug_override=None, *, optimization=None)
Given the path to a .py file, return the path to its .pyc file.
The .py file does not need to exist; this simply returns the path to the
.pyc file calculated as if the .py file were imported.
The 'optimization' parameter controls the presumed optimization level of
the bytecode file. If 'optimization' is not None, the string representation
of the argument is taken and verified to be alphanumeric (else ValueError
is raised).
The debug_override parameter is deprecated. If debug_override is not None,
a True value is the same as setting 'optimization' to the empty string
while a False value is equivalent to setting 'optimization' to '1'.
If sys.implementation.cache_tag is None then NotImplementedError is raised.
contextmanager
contextmanager(func)
@contextmanager decorator.
Typical usage:
@contextmanager
def some_generator(<arguments>):
<setup>
try:
yield <value>
finally:
<cleanup>
This makes this:
with some_generator(<arguments>) as <variable>:
<body>
equivalent to this:
<setup>
try:
<variable> = <value>
<body>
finally:
<cleanup>
decode_source
decode_source(source_bytes)
Decode bytes representing source code and return the string.
Universal newline support is used in the decoding.
find_spec
find_spec(name, package=None)
Return the spec for the specified module.
First, sys.modules is checked to see if the module was already imported. If
so, then sys.modules[name].__spec__ is returned. If that happens to be
set to None, then ValueError is raised. If the module is not in
sys.modules, then sys.meta_path is searched for a suitable spec with the
value of 'path' given to the finders. None is returned if no spec could
be found.
If the name is for submodule (contains a dot), the parent module is
automatically imported.
The name and package arguments work the same as importlib.import_module().
In other words, relative module names (with leading dots) work.
module_for_loader
module_for_loader(fxn)
Decorator to handle selecting the proper module for loaders.
The decorated function is passed the module to use instead of the module
name. The module passed in to the function is either from sys.modules if
it already exists or is a new module. If the module is new, then __name__
is set the first argument to the method, __loader__ is set to self, and
__package__ is set accordingly (if self.is_package() is defined) will be set
before it is passed to the decorated function (if self.is_package() does
not work for the module it will be set post-load).
If an exception is raised and the decorator created the module it is
subsequently removed from sys.modules.
The decorator assumes that the decorated function takes the module name as
the second argument.
module_from_spec
module_from_spec(spec)
Create a module based on the provided spec.
resolve_name
resolve_name(name, package)
Resolve a relative module name to an absolute one.
set_loader
set_loader(fxn)
Set __loader__ on the returned module.
This function is deprecated.
set_package
set_package(fxn)
Set __package__ on the returned module.
This function is deprecated.
source_from_cache
source_from_cache(path)
Given the path to a .pyc. file, return the path to its .py file.
The .pyc file does not need to exist; this simply returns the path to
the .py file calculated to correspond to the .pyc file. If path does
not conform to PEP 3147/488 format, ValueError will be raised. If
sys.implementation.cache_tag is None then NotImplementedError is raised.
source_hash
source_hash(source_bytes)
Return the hash of *source_bytes* as used in hash-based pyc files.
spec_from_file_location
spec_from_file_location(name, location=None, *, loader=None, submodule_search_locations=<object object at 0x7ff3609c60d0>)
Return a module spec based on a file location.
To indicate that the module is a package, set
submodule_search_locations to a list of directory paths. An
empty list is sufficient, though its not otherwise useful to the
import system.
The loader must take a spec as its only __init__() arg.
spec_from_loader
spec_from_loader(name, loader, *, origin=None, is_package=None)
Return a module spec based on various loader methods.
Other members
MAGIC_NUMBER = b'a\r\r\n'
Modules
abc
functools
sys
types
warnings