On this page
runpy.run_path
Prev: runpy.run_module | Up: runpy
Rel: runpy.run_module
Ref: docs.python.org — runpy.run_path
Tags: #public #python3 #stdlib #runpy
Import
import runpy
from runpy import run_path
Signature & Overview
runpy.run_path(path_name, init_globals=None, run_name=None)
Execute code located at the specified filesystem location.
Usage
from pathlib import Path
import runpy, tempfile
with tempfile.TemporaryDirectory() as tmp:
p = Path(tmp) / "cfg.py"
p.write_text("VALUE = 42\n", encoding="utf-8")
ns = runpy.run_path(str(p))
assert ns["VALUE"] == 42
Gotchas
- Executes arbitrary Python: Only run trusted scripts.
REPL Inspect
from runpy import run_path
help(run_path)
Linked from
Related notes
- tuts/python3/stdlib_module/runpy/run_module Linked here; links to this page; shared tags: #runpy, #stdlib
- tuts/python3/stdlib_module/runpy Linked here; links to this page; shared tags: #stdlib
- tuts/c/stdlib shared tags: #stdlib
- tuts/c/stdlib/assert shared tags: #stdlib
- tuts/c/stdlib/assert/assert shared tags: #stdlib
- tuts/c/stdlib/assert/static_assert shared tags: #stdlib
- tuts/c/stdlib/ctype shared tags: #stdlib
- tuts/c/stdlib/ctype/isalpha shared tags: #stdlib
- tuts/c/stdlib/ctype/isdigit shared tags: #stdlib
- tuts/c/stdlib/ctype/isspace shared tags: #stdlib