On this page
  1. runpy.run_path
  2. Import
  3. Signature & Overview
  4. Usage
  5. Gotchas
  6. REPL Inspect

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

REPL Inspect

from runpy import run_path
help(run_path)

Linked from

Related notes