7. runpy — Locating and executing Python modules

New in version 2.5.

Source code: :source:`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.

The runpy module provides two functions:

runpy.run_module(mod_name, init_globals=None, run_name=None, alter_sys=False)[source]
runpy.run_path(file_path, init_globals=None, run_name=None)[source]

See also

PEP 338 – Executing modules as scripts
PEP written and implemented by Nick Coghlan.
PEP 366 – Main module explicit relative imports
PEP written and implemented by Nick Coghlan.

using-on-general - CPython command line details