Python stderr: Printing with sys.stderr and the file kwarg

Python stderr: Printing with sys.stderr and the file kwarg

Want to print to stderr, not stdout, in a Python program? Use sys.stderr and the “file” kwarg:

>>> import sys
>>> print('Normal text')
Normal text
>>> print('Warning text', file=sys.stdout)
Warning text

stdout and stderr often look identical in a terminal — but they aren’t!