On this page
subprocess.CalledProcessError
Prev: subprocess.call | Up: subprocess | Next: subprocess.check_call
Rel: subprocess.call, subprocess.check_call, subprocess.check_output, subprocess.CompletedProcess
Ref: docs.python.org — subprocess.CalledProcessError
Tags: #public #python3 #stdlib #subprocess
Import
import subprocess
from subprocess import CalledProcessError
Signature & Overview
subprocess.CalledProcessError(returncode, cmd, output=None, stderr=None)
Raised when run() is called with check=True and the process returns a non-zero exit status.
Methods & Attributes
CalledProcessError.add_note(self, note, /)— Add a note to the exceptionCalledProcessError.stdout(...)— Alias for output attribute, to match stderrCalledProcessError.with_traceback(self, tb, /)— Set self.traceback to tb and return self.
Usage
import subprocess
import sys
try:
subprocess.run([sys.executable, "-c", "raise SystemExit(2)"], check=True, capture_output=True, text=True)
except subprocess.CalledProcessError as exc:
assert exc.returncode == 2
Gotchas
- Raised when
check=True(orcheck_call/check_output) sees a non-zero exit code: Inspectexc.returncode,exc.cmd,exc.stdout, andexc.stderr. stderrisNoneunless captured: Passcapture_output=True(orstderr=subprocess.PIPE) if you wantexc.stderrpopulated.
REPL Inspect
from subprocess import CalledProcessError
help(CalledProcessError)
Linked from
- tuts/python3/stdlib_module/subprocess
- tuts/python3/stdlib_module/subprocess/call
- tuts/python3/stdlib_module/subprocess/check_call
- tuts/python3/stdlib_module/subprocess/check_output
- tuts/python3/stdlib_module/subprocess/CompletedProcess
- tuts/python3/stdlib_module/subprocess/getoutput
- tuts/python3/stdlib_module/subprocess/getstatusoutput
- tuts/python3/stdlib_module/subprocess/Popen
- tuts/python3/stdlib_module/subprocess/run
- tuts/python3/stdlib_module/subprocess/SubprocessError
- tuts/python3/stdlib_module/subprocess/TimeoutExpired
Related notes
- tuts/python3/stdlib_module/subprocess/call Linked here; links to this page; shared tags: #stdlib, #subprocess
- tuts/python3/stdlib_module/subprocess/check_call Linked here; links to this page; shared tags: #stdlib, #subprocess
- tuts/python3/stdlib_module/subprocess/check_output Linked here; links to this page; shared tags: #stdlib, #subprocess
- tuts/python3/stdlib_module/subprocess/CompletedProcess 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/getoutput links to this page; shared tags: #stdlib, #subprocess
- tuts/python3/stdlib_module/subprocess/getstatusoutput links to this page; shared tags: #stdlib, #subprocess
- tuts/python3/stdlib_module/subprocess/Popen links to this page; shared tags: #stdlib, #subprocess
- tuts/python3/stdlib_module/subprocess/run links to this page; shared tags: #stdlib, #subprocess
- tuts/python3/stdlib_module/subprocess/SubprocessError links to this page; shared tags: #stdlib, #subprocess