# pydoc > runpy

---
type: CommandReference
command: runpy
mode: pydoc
section: 
source: pydoc3
---

## Quick Reference
- `runpy.run_module('modname')` — Execute a module as `__main__`
- `runpy.run_module('modname', run_name='__main__')` — Force `__name__` to `'__main__'`
- `runpy.run_module('modname', alter_sys=True)` — Mimic script execution (update `sys.argv[0]` and `sys.modules`)
- `runpy.run_path('/path/to/script.py')` — Run a script from the filesystem
- `runpy.run_path('/path/to/dir/')` — Run a directory containing `__main__.py`
- `runpy.run_path('archive.zip')` — Run a zipfile containing `__main__.py`
- `globals_dict = runpy.run_module('modname', init_globals={'var': value})` — Pre-populate module globals and capture result

## Name
`runpy` — Locate and run Python code using the module namespace.

## Synopsis
python
import runpy
runpy.run_module(mod_name, init_globals=None, run_name=None, alter_sys=False)
runpy.run_path(path_name, init_globals=None, run_name=None)
## Functions

### `runpy.run_module(mod_name, init_globals=None, run_name=None, alter_sys=False)`
Execute a module’s code without importing it. Returns the resulting module globals dictionary.

- `mod_name` — Absolute module name or package name.
- `init_globals` — Dictionary to pre-populate the module’s globals before execution.
- `run_name` — If not `None`, sets `__name__`; otherwise automatic: for a package, `mod_name + '__main__'`, else `mod_name`.
- `alter_sys` — If `True`, temporarily sets `sys.argv[0]` to `__file__` and `sys.modules[__name__]` to a temporary module object. Both are restored before returning.

### `runpy.run_path(path_name, init_globals=None, run_name=None)`
Execute code at the specified filesystem location. Returns the resulting module globals dictionary.

- `path_name` — Filesystem location: a Python script, a zipfile, or a directory containing a top‑level `__main__.py`.
- `init_globals` — Same as in `run_module`.
- `run_name` — If not `None`, sets `__name__`; otherwise `'<run_path>'` is used.

## Examples

python
# Run a module by name
result = runpy.run_module('mymodule')

# Pre-populate globals and capture the result
globs = runpy.run_module('mypackage', init_globals={'DEBUG': True})

# Run a script with altered sys.argv[0]
runpy.run_module('myscript', alter_sys=True)

# Run a script from a path
runpy.run_path('/home/user/scripts/tool.py')

# Run a directory (looks for __main__.py)
runpy.run_path('/home/user/project/')
## See Also
- [Python documentation for `runpy`](https://docs.python.org/3.10/library/runpy.html)
- [`importlib`](https://docs.python.org/3.10/library/importlib.html) — The implementation of the import machinery.
- [`__main__`](https://docs.python.org/3.10/library/__main__.html) — The environment where top-level code is run.