---
source: https://iinventedlight.com/library/notes/python-runpy.md
title: "runpy: locate and run Python modules without importing first"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "PSF License Version 2 (Python Software Foundation); PSF License Version 2 (CPython); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: Python Software Foundation, runpy — Locating and executing Python modules; CPython, runpy documentation (source). PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-23
status: published
approved_by: "Cole Withington, 2026-11-27 14:30 PT: \"Yes, publish all of that now\""
checked_on: 2026-11-23
as_of: 2026-11-23
confidence: medium
untested: "Not reproduced (2026-11-23). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-python: python-runpy"
tags: [python, runpy, modules, command-line, stdlib]
client: Librarian
sources:
  - id: S1
    title: "runpy — Locating and executing Python modules"
    publisher: Python Software Foundation
    url: https://docs.python.org/3/library/runpy.html
    accessed: 2026-11-23
    license: "PSF License Version 2"
  - id: S2
    title: "runpy documentation (source)"
    publisher: CPython
    url: https://raw.githubusercontent.com/python/cpython/main/Doc/library/runpy.rst
    accessed: 2026-11-23
    license: "PSF License Version 2"
---
# runpy: locate and run Python modules without importing first
**Answer:** The runpy module locates and runs Python modules without importing them first. Its main use is the -m command line switch. This is not a sandbox: code runs in the current process, and side effects remain.

## Why it exists

Use this module to locate and run Python modules without importing them first.

The main use is the -m command line switch. Scripts can be located using the Python module namespace rather than the filesystem.

It is not a sandbox. Code runs in the current process, and side effects stay after the functions return.

## The two functions

run_module runs a named module and returns that module's globals dictionary.

run_path runs the code at a filesystem location and also returns a globals dictionary.

run_module accepts mod_name, init_globals, run_name, and alter_sys.

## See also

- [python-webbrowser](https://iinventedlight.com/library/notes/python-webbrowser/)
- [python-sysconfig](https://iinventedlight.com/library/notes/python-sysconfig/)

## Claims

- id: C1
  claim: runpy locates and runs modules without importing them first.
  source: S1
  evidence: "locate and run Python modules without"

- id: C2
  claim: Its main use is to implement the -m command line switch.
  source: S1
  evidence: "implement the -m command line switch"

- id: C3
  claim: It is not a sandbox; code runs in the current process.
  source: S1
  evidence: "not a sandbox module"

- id: C4
  claim: run_module executes a module and returns its globals dictionary.
  source: S2
  evidence: "Execute the code of the specified module and return the resulting module"

## Sources

- [S1] runpy — Locating and executing Python modules. Python Software Foundation. https://docs.python.org/3/library/runpy.html . License: PSF License Version 2. Accessed 2026-11-23.

- [S2] runpy documentation (source). CPython. https://raw.githubusercontent.com/python/cpython/main/Doc/library/runpy.rst . License: PSF License Version 2. Accessed 2026-11-23.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python Software Foundation, runpy — Locating and executing Python modules; CPython, runpy documentation (source). PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Text was condensed and changed from the originals.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
