On this page
subprocess.run
Prev: subprocess.Popen | Up: subprocess | Next: subprocess.SubprocessError
Rel: subprocess.call, subprocess.CalledProcessError, subprocess.check_call, subprocess.check_output
Ref: docs.python.org — subprocess.run
Tags: #public #python3 #stdlib #subprocess
Import
import subprocess
from subprocess import run
Signature & Overview
subprocess.run(*popenargs, input=None, capture_output=False, timeout=None, check=False, **kwargs)
Run command with arguments and return a CompletedProcess instance.
Usage
import subprocess
import sys
res = subprocess.run(
[sys.executable, "-c", "print(2 + 2)"],
capture_output=True,
text=True,
check=True,
timeout=10,
)
assert res.stdout.strip() == "4"
Gotchas
- Pass an argument list, not a shell string: Passing
["git", "status"]avoids shell injection and quoting bugs (shell=Falseis the safe default). - Always consider
check=Trueandtimeout=: Withoutcheck=True, non-zero exit codes succeed silently; withouttimeout=, a hung subprocess blocks forever.
REPL Inspect
from subprocess import run
help(run)
Linked from
Related notes
- tuts/python3/stdlib_module/subprocess/Popen Linked here; links to this page; shared tags: #stdlib, #subprocess
- tuts/python3/stdlib_module/subprocess/SubprocessError Linked here; links to this page; shared tags: #stdlib, #subprocess
- tuts/python3/stdlib_module/subprocess Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/subprocess/call Linked here; shared tags: #stdlib, #subprocess
- tuts/python3/stdlib_module/subprocess/CalledProcessError Linked here; shared tags: #stdlib, #subprocess
- tuts/python3/stdlib_module/subprocess/check_call Linked here; shared tags: #stdlib, #subprocess
- tuts/python3/stdlib_module/subprocess/check_output Linked here; shared tags: #stdlib, #subprocess
- tuts/python3/stdlib_module/subprocess/CompletedProcess shared tags: #stdlib, #subprocess
- tuts/python3/stdlib_module/subprocess/getoutput shared tags: #stdlib, #subprocess
- tuts/python3/stdlib_module/subprocess/getstatusoutput shared tags: #stdlib, #subprocess