How to Print to stderr in Python

If you want to print out to the standard error stderr pipe in Python, you can make use of the sys.stderr.write() function from the sys module.


Example:
>>> import sys
>>> message = "I want to print this in standard error - stderr!"
>>> sys.stderr.write(message)

48
I want to print this in standard error - stderr!>>> 

If you are using Python 3+ you can make use of the print function and pass in an option file=sys.stderr to redirect the data to the standard error pipe.

Print to stderr in Python

Reference:

1. https://docs.python.org/3/library/functions.html#print

2. https://docs.python.org/3/library/sys.html

Comments & Discussion

Facing issues? Have questions? Post them here! We're happy to help!