Skip to content

stderr

Short for standard error, stderr is the output stream that programs use for diagnostic and error messages, kept separate from normal output so the two can be redirected and processed independently.

Standard error is one of three preconnected I/O streams that a process inherits from the process that started it, alongside stdin and stdout, and it occupies file descriptor 2. By default, it writes to the controlling terminal, which keeps error messages visible even when stdout is piped into another command or redirected to a file.

Keeping diagnostics on their own stream is a deliberate design that dates back to early Unix in the 1970s. POSIX also requires that stderr not be fully buffered, so diagnostics appear promptly even if the program exits abnormally before flushing its other streams.

Modern shells expose stderr through redirection syntax such as 2> and 2>&1. For example, you can send a program’s normal output to a file while its errors still surface on the terminal:

Language: Shell
$ ls main.py missing.py > files.txt
ls: cannot access 'missing.py': No such file or directory
$ cat files.txt
main.py

Here, the ls command writes the name it found to standard output, which the shell redirects into files.txt, but the message about the missing file goes to stderr and still prints to the terminal. Adding 2> errors.txt to the command would capture those diagnostics in their own file instead.

In Python, you write to standard error through sys.stderr, or by passing file=sys.stderr to the print() function so a message goes to the error stream instead of normal output.

Tutorial

Logging in Python

If you use Python's print() function to get information about the flow of your programs, logging is the natural next step. Create your first logs and curate them to grow with your projects.

intermediate best-practices stdlib tools

For additional information on related topics, take a look at the following resources:


By Martin Breuss • Updated June 10, 2026 • Reviewed by Leodanis Pozo Ramos