Skip to main content

How to Print to stderr and stdout in Python

In Python, as in most programming languages, there are distinct channels or "streams" for handling input and output. The two primary output streams are standard output (stdout) and standard error (stderr). By default, the print() function sends its output to stdout, while Python tracebacks and exceptions are sent to stderr.

Understanding how to control these streams is essential for good application design, especially for logging, debugging, and creating command-line tools. This guide will show you how to explicitly write to both stdout and stderr using several common methods.

Understanding Standard Streams: stdout vs. stderr

  • stdout (Standard Output): This is the default stream for normal program output. It's where the results of your program's successful execution should go.
  • stderr (Standard Error): This stream is reserved for error messages, warnings, and diagnostic information.

Separating these streams is a powerful feature of command-line applications. It allows users to redirect normal output to a file while still seeing error messages on the screen.

note

Example of Redirection in a Shell:

  • python my_script.py > output.txt: This saves the stdout to output.txt, but stderr will still print to the console.
  • python my_script.py 2> error.log: This saves the stderr to error.log, but stdout will still print to the console.

Printing to Standard Output (stdout)

Using the print() Function

The print() function is the standard, high-level way to write to stdout. It automatically converts objects to strings, separates arguments with spaces, and adds a newline character at the end.

Solution:

print("Hello World!", "This is a normal message.")

Output:

Hello World! This is a normal message.

Using sys.stdout.write()

For more low-level control, you can write directly to the stdout stream using the sys module.

Solution:

import sys

# .write() does not add spaces or a newline character.
sys.stdout.write("Hello World! ")
sys.stdout.write("This is a normal message.\n") # You must add the newline manually

Output:

Hello World! This is a normal message.
warning

The sys.stdout.write() method requires its argument to be a string and returns the number of characters written. It is less convenient than print() for general use.

Printing to Standard Error (stderr)

Using print(file=sys.stderr)

The easiest and most Pythonic way to print a simple message to stderr is to use the file parameter of the built-in print() function.

Solution:

import sys

print("This is an error message.", file=sys.stderr)

Output (sent to the stderr stream):

This is an error message.

Using sys.stderr.write()

Similar to stdout, you can also write directly to the stderr stream for low-level control.

Solution:

import sys

sys.stderr.write("This is an error message.\n")

Output (sent to the stderr stream):

This is an error message.

Using the logging Module (Best Practice for Errors)

For any real application, the logging module is the recommended way to handle warnings, errors, and other diagnostic messages. By default, messages of level WARNING and higher are sent to stderr.

This approach is far more flexible than print() because it allows for configurable log levels, formatting, and output destinations (e.g., files, network sockets).

Solution:

import logging

logging.warning("This is a warning message.")
logging.error("This is an error message.")

Output (sent to the stderr stream, format may vary):

WARNING:root:This is a warning message.
ERROR:root:This is an error message.

Conclusion

MethodDestinationBest For
print()stdoutGeneral program output for the user.
print(file=sys.stderr)stderrQuick and simple error or debug messages.
logging modulestderr (by default for warnings/errors)Robust application-level logging. This is the recommended practice for all non-trivial programs.
sys.stdout.write() / sys.stderr.write()stdout/stderrLow-level control where automatic newlines and type conversion are not desired.

By using the correct stream for your output, you can create more powerful and flexible command-line applications that follow standard conventions. For error reporting, the logging module should be your default choice.