Standard Library
Subprocesses
subprocess.run() spawns a child Python interpreter and waits for it: capture_output=True stores the child's stdout and stderr on the result, text=True decodes them as strings, and check=True raises CalledProcessError on a non-zero exit. The result object carries the captured streams and exit code as portable evidence the child ran. The in-browser Run button cannot spawn processes, so pressing Run here fails in the sandbox; the output below was produced by really spawning the child under standard CPython when the example was verified.
Source
import subprocess
import sys
result = subprocess.run(
[sys.executable, "-c", "print('child process')"],
text=True,
capture_output=True,
check=True,
)
print(result.stdout.strip())
print(result.returncode)Output
child process
0Notes
- Use a list of arguments instead of shell strings when possible.
- Capture output when the parent program needs to inspect it.
check=Trueturns non-zero exits into exceptions.- The verified output came from a real child process under standard CPython at build time; the in-browser sandbox has no process table, so live runs of this page fail there.
See also
- prerequisite: Virtual Environments
- related: Networking
- related: Threads and Processes
Run the complete example
Expected output
child process
0
Execution time appears here after you run the example.