← All notes

subprocess.run is the recommended way to spawn a process in Python

Answer: The subprocess module allows you to spawn new processes. It can connect to their input/output/error pipes, and obtain their return codes. For use cases it can handle, the recommended approach is the run function.

subprocess.run(["ls", "-l"]) does not capture output and returns a CompletedProcess. subprocess.run(["ls", "-l", "/dev/null"], capture_output=True) captures output. The docs also show subprocess.run("exit 1", shell=True, check=True) raising CalledProcessError.

This module intends to replace several older modules and functions, including os.system and os.spawn*.

See also

Claims

Sources

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python docs, subprocess — Subprocess management, PSF-2.0 (https://docs.python.org/3/license.html); python/cpython (GitHub), subprocess.rst (CPython docs), PSF-2.0 (https://raw.githubusercontent.com/python/cpython/main/LICENSE). Text was condensed and changed from the originals.

License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Python docs, subprocess — Subprocess management, PSF-2.0 (https://docs.python.org/3/license.html); python/cpython (GitHub), subprocess.rst (CPython docs), PSF-2.0 (https://raw.githubusercontent.com/python/cpython/main/LICENSE).

Cite this

Permanent ID: iil:note:7w6mgdn5

iinventedlight. 2026. "subprocess.run is the recommended way to spawn a process in Python." iinventedlight Library, iil:note:7w6mgdn5, version of 2026-11-16. https://iinventedlight.com/library/notes/python-subprocess/. License: CC-BY-4.0.

@misc{iil_note_7w6mgdn5,
  author = {{iinventedlight}},
  title = {{subprocess.run is the recommended way to spawn a process in Python}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-16},
  url = {https://iinventedlight.com/library/notes/python-subprocess/},
  note = {Permanent ID iil:note:7w6mgdn5. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).

For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/python-subprocess.md.