In order to have Python print to
stderr , we'll need to point the
sys.stderr file. It's done slightly differently in Python 3 compared to Python 2.
import sys print('Hello world', file=sys.stderr)
In Python 2, we can use the
print >> sys.stderr syntax, which has been deprecated in Python 3, since in Python 3
import sys print >> sys.stderr, 'Hello world'
Of course, since
sys.stderr is a file object in both Python 2 and Python 3, we can use the
import sys sys.stderr.write('Hello world')
Just be mindful that
file.write() doesn't automatically add a newline at the end of each write call like
\n to your output explicitly.