all Python 3subprocessfunction
subprocess.Popen()
running processes
Low-level process launching, managed by hand. It lets you read stdout/stderr as they come and write to stdin.
Syntax
proc = subprocess.Popen(args, stdin=, stdout=, stderr=, text=False, ...)
proc.communicate(input=None)
proc.wait()
Popen returns immediately: the child keeps running in parallel and there is no exit code until you call wait() or communicate(). Draining stdout and stderr by hand, one after the other, when both are pipes will deadlock as soon as an OS buffer fills up; communicate() reads both at the same time and avoids that trap. If you only need the finished result rather than a live conversation with the process, reach for subprocess.run() instead.
Examples
import subprocess
proc = subprocess.Popen(
['python3', '-c', 'print("hello")'],
stdout=subprocess.PIPE, text=True
)
out, err = proc.communicate()
print(out) # hello
print(proc.returncode) # 0
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy