Why does python -V write to the error stream?

I wrote a script to check the version of python on my system, and I noticed that python -V writes to the error stream, and python -h , for example, uses standard output. Is there a good reason for this behavior?

+6
python
source share
4 answers

The -h option is also used to print to stderr, since it is not part of your program output, i.e. the result is not created by your Python script, but the Python interpreter itself.

As for why they changed -h to use stdout? Try entering python -h when your terminal window is set to the standard 24 lines. It scrolls from the screen.

Now most people would respond by trying python -h |less , but this only works if you send the -h output to stdout instead of stderr. Therefore, there was a good reason for creating -h to go to stdout, but there is no good reason to change -V.

+3
source share

-h is used to print in stderr too, as you see here from main.c

 usage(int exitcode, char* program) { fprintf(stderr, usage_line, program); fprintf(stderr, usage_top); fprintf(stderr, usage_mid); fprintf(stderr, usage_bot, DELIM, DELIM, PYTHONHOMEHELP); exit(exitcode); /*NOTREACHED*/ } ... if (help) usage(0, argv[0]); if (version) { fprintf(stderr, "Python %s\n", PY_VERSION); exit(0); 

Current main.c has changed the way it is used

 usage(int exitcode, char* program) { FILE *f = exitcode ? stderr : stdout; fprintf(f, usage_line, program); if (exitcode) fprintf(f, "Try `python -h' for more information.\n"); else { fputs(usage_1, f); fputs(usage_2, f); fputs(usage_3, f); fprintf(f, usage_4, DELIM); fprintf(f, usage_5, DELIM, PYTHONHOMEHELP); } 

So usage uses stdout for -h and stderr for -Q.

I see no evidence of a good reason in one of the other ways. Perhaps it cannot be changed now without breaking backward compatibility.

+2
source share

Why?

Because this is not the actual output of your actual script.

This is a standard, common, common, standard use of a standard error: everything is NOT output from your script.

+2
source share

Probably not without reason, some digging showed a patch adding parameters, but I can find any links to why different threads are used in the discussion of the patch.

+1
source share

All Articles