PyDocs

Nav

Importing Modules

run_module

runpy

function (source)
  • run_module(mod_name, init_globals=None, run_name=None, alter_sys=False)
Execute a module's code without importing it.

mod_name -- an absolute module name or package name.

Optional arguments:
init_globals -- dictionary used to pre-populate the module’s
globals dictionary before the code is executed.

run_name -- if not None, this will be used for setting __name__;
otherwise, __name__ will be set to mod_name + '__main__' if the
named module is a package and to just mod_name otherwise.

alter_sys -- if True, sys.argv[0] is updated with the value of
__file__ and sys.modules[__name__] is updated with a temporary
module object for the module being executed. Both are
restored to their original values before the function returns.

Returns the resulting module globals dictionary.
Changed in 3.1, 3.2, 3.4, 3.12, 3.15
  • 3.1: Added ability to execute packages by looking for a __main__ submodule.
  • 3.2: Added __cached__ global variable (see PEP 3147).
  • 3.4: Updated to take advantage of the module spec feature added by PEP 451. This allows __cached__ to be set correctly for modules run this way, as well as ensuring the real module name is always accessible as __spec__.name.
  • 3.12: The setting of __cached__, __loader__, and __package__ are deprecated. See ModuleSpec for alternatives.
  • 3.15: The global variable __cached__ is no longer set.
0 Examples
No examples for runpy/run_module.
See Also
No see-alsos for runpy/run_module
0 Notes
No notes for run_module