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?

Ed Morton
  • 188,023
  • 17
  • 78
  • 185
  • 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? – JussiV Jun 07 '18 at 07:21
  • Also, you could find version in the following way: `python -c "import sys; print(sys.version)" > version` – Yaroslav Surzhikov Jun 07 '18 at 07:26
  • @JussiV it turned out that's the actual problem.. but TBH, who would know that it prints out version to stderr. facepalming... – Haoming Yin Jun 07 '18 at 07:35
  • @HaomingYin: `python` is not alone, `ksh` (Korn shell), `gcc` and `clang` do the same, and there are probably others. – cdarke Jun 07 '18 at 08: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. – JussiV Jun 07 '18 at 09:46

2 Answers2

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}'
Ilja Everilä
  • 50,538
  • 7
  • 126
  • 127
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.

RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93