Python 3.3+Exceptionsexception
BrokenPipeError
built-in exceptions
Writing to a pipe/socket closed on the other end (a subclass of ConnectionError).
Syntax
raise BrokenPipeError
The classic case: your script prints a lot of lines and the output is piped into head — the reader goes away, and the next write blows up. The error is about the peer, not about your data, so retrying the write is pointless; close the connection or open a new one. It sometimes surfaces only at interpreter shutdown, when the stdout buffer is flushed, which makes it look like an error out of nowhere.
Examples
try:
raise BrokenPipeError
except BrokenPipeError as e:
print(type(e).__name__) # → BrokenPipeError
See also
Open in the full glossarydocs.python.orgReport an inaccuracy