Python 3.3+Exceptionsexception
subprocess.SubprocessError
module exceptions
The base class of the subprocess module's own exceptions: CalledProcessError and TimeoutExpired inherit from it.
Syntax
except subprocess.SubprocessError:
It does not cover everything that can go wrong at launch: a missing program or missing permissions make Popen raise FileNotFoundError or PermissionError, which are OSError, not SubprocessError. To catch launch failures reliably use except (subprocess.SubprocessError, OSError). It derives straight from Exception, so TimeoutExpired is not caught by the built-in TimeoutError either.
Examples
import subprocess
print(issubclass(subprocess.TimeoutExpired, subprocess.SubprocessError)) # → True
print(issubclass(FileNotFoundError, subprocess.SubprocessError)) # → False
import subprocess
try:
subprocess.run(['no-such-program-xyz'])
except (subprocess.SubprocessError, OSError) as e:
print(type(e).__name__) # → FileNotFoundError
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy