Asked • 05/24/19

How to print to stderr in Python?

There are several ways to write to stderr: # Note: this first one does not work in Python 3 print >> sys.stderr, "spam" sys.stderr.write("spam\\n") os.write(2, b"spam\\n") from __future__ import print_function print("spam", file=sys.stderr)That seems to contradict *zen of Python #13* <sup>†</sup>, so what's the difference here and are there any advantages or disadvantages to one way or the other? Which way should be used?<sup>†</sup> <sub>There should be one — and preferably only one — obvious way to do it.</sub>

1 Expert Answer


Hayward B. answered • 07/31/20

New to Wyzant

python developer, software enginneer

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.


Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.