Python runpy Module
Example
Run a module as a script (like python -m):
import runpy
# Run the http.server module
result = runpy.run_module('http.server', run_name='__main__')
print('Module executed')
Try it Yourself »
Definition and Usage
The runpy module locates and executes Python modules without importing them first.
Use it to run modules as scripts (equivalent to python -m), execute code from files, or programmatically run Python packages.
Members
Member | Description |
---|---|
run_module() | Execute a module's code and return the module's globals dictionary. |
run_path() | Execute code at a filesystem location and return the module's globals. |