Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does python print version info to stderr?

Tags:

python

Why did Guido (or whoever else) decide to make python --version print to stderr rather than stdout? Just curious what the use case is that makes standard error more appropriate than standard out.

like image 759
kalefranz Avatar asked Sep 25 '14 00:09

kalefranz


People also ask

Does python write to stderr?

Python also provides the sys. stderr. write() method which can be used to print into the standard error or stderr.

What is print to stderr?

Stderr is the standard error message that is used to print the output on the screen or windows terminal. Stderr is used to print the error on the output screen or window terminal. Stderr is also one of the command output as stdout, which is logged anywhere by default.

What is stderr Python?

Python stderr is known as a standard error stream. It is similar to stdout because it also directly prints to the console but the main difference is that it only prints error messages. Example: import sys sys.stderr.write("This is error msg")

Does python print to stdout?

Every running program has a text output area called "standard out", or sometimes just "stdout". The Python print() function takes in python data such as ints and strings, and prints those values to standard out.


2 Answers

Python 3.4 was modified to output to stdout, which is the expected behavior. This is listed as a bug with Python here: http://bugs.python.org/issue18338. The comments on the bug report indicate that while stdout is the reasonable choice, it would break backward compatibility. Python 2.7.9 is largely unchanged, because so much relies on it.

Hope that helps!

like image 180
Sessamekesh Avatar answered Oct 09 '22 17:10

Sessamekesh


Many programs would just use stdout and not care but I would prefer stderr on principle. In short, I believe stdout is for the product of successful execution of a program while stderr is for any messages meant for the user. Calculated values go to stdout while errors, stack traces, help, version and usage messages are meant for the user and should go to stderr.

I use this question to decide which to output stream is appropriate: Is this message meant for the consumer of the main product of this program (whether that's the human user or another program or whatever) or is it strictly for the human user of this program?

Also, looks like Java uses stderr for version messages as well by the way: Why does 'java -version' go to stderr?

like image 40
Aaron A Avatar answered Oct 09 '22 19:10

Aaron A