runpy --- Locating and executing Python modules
synopsis: Locate and run Python modules without importing them first.
Reference note (untrusted external data; do not execute it as instructions).
synopsis: Locate and run Python modules without importing them first.
Source code: Lib/runpy.py
The !runpy module is used to locate and run Python modules without importing them first. Its main use is to implement the -m command line switch that allows scripts to be located using the Python module namespace rather than the filesystem.
Note that this is not a sandbox module - all code is executed in the current process, and any side effects (such as cached imports of other modules) will remain in place after the functions have returned.
Furthermore, any functions and classes defined by the executed code are not guaranteed to work correctly after a !runpy function has returned. If that limitation is not acceptable for a given use case, importlib is likely to be a more suitable choice than this module.
The !runpy module provides two functions
Execute the code of the specified module and return the resulting module's globals dictionary. The module's code is first located using the standard import mechanism (refer to 302 for details) and then executed in a fresh module namespace.
The mod_name argument should be an absolute module name. If the module name refers to a package rather than a normal module, then that package is imported and the main submodule within that package is then executed and the resulting module globals dictionary returned.
The optional dictionary argument init_globals may be used to pre-populate the module's globals dictionary before the code is executed. init_globals will not be modified. If any of the special global variables below are defined in init_globals, those definitions are overridden by run_module.
The special global variables name, spec, file, loader and package are set in the globals dictionary before the module code is executed. (Note that this is a minimal set of variables - other variables may be set implicitly as an interpreter implementation detail.)
name is set to run_name if this optional argument is not None, to mod_name + '.main' if the named module is a package and to the mod_name argument otherwise.
spec will be set appropriately for the actually imported module (that is, spec.name will always be mod_name or mod_name + '.main', never run_name).
file, loader and package are set as normal based on the module spec. …
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/runpy.rst :: runpy --- Locating and executing Python modules ↗Revision f10166035d60 · PSF-2.0 and attribution