4

I was trying to write a bash script to test the python version. However, I found python --version behave weirdly for python 2, as I can't process its output using any tool. I have tested the same script on Mac OS (10.13.5) and AWS Linux (GUN/Linux #1 SMP Fri Feb 16 00:18:48 UTC 2018). So I think the problem is related to python 2.

The script and corresponding output are:

$ echo $(python --version) | awk '{print $2}'
> Python 2.7.10

But the output should be 2.7.10.

$ echo $(python --version) > weird.txt
> Python 2.7.10
$ cat weird.txt
>

So the output cannot be written into a file as well.

The same script to test for python3 has a totally different result

$ echo $(python3 --version) | awk '{print $2}'
> 3.6.5

But python3's output can be written into a file.

$ echo $(python3 --version) > weird.txt
$ cat weird.txt
> Python 3.6.5

I have found the reason for this difference is that python --version does not output a normal string or whatsoever. Maybe it calls another command to output the result for it??? Thus the result cannot be caught by current process?? (just pure guess here)

Can anyone help me to figure out why there is the difference here? There are probably of million ways to test for python version. But I'm just super curious about what is happening here.

Thanks for all the replies. Just found a useful answer explaining that why python -V outputs to stderr: Why does python print version info to stderr?

5
  • 4
    I'm not 100% sure but aren't you only capturing the stdout and ignoring stderr? What happens if you add 2>&1 to the end of the attempted file writing? Commented Jun 7, 2018 at 7:21
  • Also, you could find version in the following way: python -c "import sys; print(sys.version)" > version Commented Jun 7, 2018 at 7:26
  • @JussiV it turned out that's the actual problem.. but TBH, who would know that it prints out version to stderr. facepalming... Commented Jun 7, 2018 at 7:35
  • @HaomingYin: python is not alone, ksh (Korn shell), gcc and clang do the same, and there are probably others. Commented Jun 7, 2018 at 8:08
  • @Inian This is not a duplicate like you flagged it, it's not about determining the version but figuring out what happens to the version output of certain Python versions. I don't know how or I lack the rep to dispute the flag, so let's try this. Commented Jun 7, 2018 at 9:46

2 Answers 2

8

Python outputs the version to standard error (stderr) up to version 3.3 according to issue 18338 and as noted here, so redirect accordingly:

$ echo $(python --version 2>&1) | awk '{print $2}'
2.7.14

The command substitution is unnecessary and this could be written as:

$ python --version 2>&1 | awk '{print $2}'
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, that's a quick reply. Any idea why do they output it to stderr? that's really confusing.
You don't need the echo here, just python --version 2>&1 | awk '{print $2}'
The reason I 'echo'ed it was that I thought it will catch everything to stdout.. but obviously I was wrong
3

How about using Python command itself using platform library of it(which is a very common in use).

python -c 'import platform; print(platform.python_version())'

When I run it I get Python's exact version.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.