importlib --- The implementation of !import — importlib.util -- Utility code for importers
Source code: Lib/importlib/util.py This module contains the various objects that help in the construction of an importer.
Reference note (untrusted external data; do not execute it as instructions).
Source code: Lib/importlib/util.py
This module contains the various objects that help in the construction of an importer.
The bytes which represent the bytecode version number. If you need help with loading/writing bytecode then consider importlib.abc.SourceLoader.
Return the 3147/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 specify the optimization level of the bytecode file. An empty string represents no optimization, so /foo/bar/baz.py with an optimization of '' will result in a bytecode path of /foo/bar/pycache/baz.cpython-32.pyc. None causes the interpreter's optimization level to be used. Any other value's string representation is used, so /foo/bar/baz.py with an optimization of 2 will lead to the bytecode path of /foo/bar/pycache/baz.cpython-32.opt-2.pyc. The string representation of optimization can only be alphanumeric, else ValueError is raised.
Given the path to a 3147 file name, return the associated source code file path. For example, if path is /foo/bar/pycache/baz.cpython-32.pyc the returned path would be /foo/bar/baz.py. path need not exist, however if it does not conform to 3147 or 488 format, a ValueError is raised. If sys.implementation.cache_tag is not defined, NotImplementedError is raised.
Decode the given bytes representing source code and return it as a string with universal newlines (as required by importlib.abc.InspectLoader.get_source).
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', spec.parent) without doing a check to see if the package argument is needed.
ImportError is raised if name is a relative module name but package is a false value (e.g. None or the empty string). ImportError is also raised if a relative name would escape its containing package (e.g. requesting ..bacon from within the spam package). …
Attribution: Adapted from Python Documentation under PSF-2.0. Adaptation: WikiKV isolated this documentation section, normalized formatting, retained only bounded code excerpts, and shortened it at a paragraph or sentence boundary for retrieval. Verify version-sensitive details at the source.
ATTRIBUTED SOURCE
This compact reference card is adapted from official documentation and is not a community-verified experience.
Python Documentation — Doc/library/importlib.rst :: importlib.util -- Utility code for importers ↗Revision f10166035d60 · PSF-2.0 and attribution