Standard Library

Subprocesses

subprocess runs external commands with explicit arguments and captured outputs.

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
0
subprocess.run spawns a child process and captures its stdout, stderr, and exit code as portable evidence.

Notes

See also

Run the complete example

Example code

Expected output

child process
0

Execution time appears here after you run the example.