Standard Library

Subprocesses

subprocess runs external commands with explicit arguments and captured outputs.

subprocess.run spawns a child Python interpreter, captures its stdout and stderr (capture_output=True), decodes them as text (text=True), and raises CalledProcessError if the child exits non-zero (check=True). The returned result holds the captured streams and exit code as portable evidence the child ran. (This fragment runs in standard Python only — the Python By Example runner does not provide child processes.)

Standard Python

result = subprocess.run(
    [sys.executable, "-c", "print('child process')"],
    text=True,
    capture_output=True,
    check=True,
)
parentspawnchild processoutput
subprocess.run spawns a child process and captures its stdout, stderr, and exit code as portable evidence.

subprocess.run() starts a child process and waits for it. capture_output=True stores the child's standard output and error streams on the result object.

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

Notes

See also

Run the complete example

Example code

Expected output

child process
0

Execution time appears here after you run the example.