Python --version without "Python " in printout

When one runs python --version (docs) it prints Python 3.10.3. This is useful as we all know.

If one doesn’t want the "Python " part, the best way is python -c "import platform; print(platform.python_version())" which prints 3.10.3.

For CI pipelines, the semantic version number alone (without "Python ") can be used to incorporate the Python version into a cache key.

The feature request is to add a flag after --version such as -t, --tag-only which will just print the version tag alone like 3.10.3.

> python --version --tag-only
3.10.3

What do people think?

Why do you feel the need for a new CLI flag when there’s a one-line with -c that does what you’re after? And is there a reason that Python 3.10.3 can’t be used in the cache key?

Hello Brett,

Thank you for the quick response!

I think the -c line detailed above involves some reinventing of the wheel and takes up quite a lot of characters (62). It would be nice for a CI pipeline file (usually YAML, which may already be quite indented) to keep the command’s length under 30 chars.

To address your other question, Python 3.10.3 also works fine! We can see the pre-commit docs use python --version --version to generate the key, which is probably more robust.

I think it would be convenient to enable acquisition of the semantic version with a CLI trick, but it is not required by any means.

There may be other uses for the semantic version being easily acquired from outside of the Python console. Looking forward to hearing back!

You likely already know, but for other’s benefit, YAML does support representing single-line strings across multiple lines: https://yaml-multiline.info/

Some (code golfing) alternative commands for comparison / fun:

python -c "import platform; print(platform.python_version())"
pyfil platform.python_version()
pyfil sys.version.split()[0]
python -V|awk '{print $2}'
python -V|perl -lae 'print $F[1]'
python -V|pyfil -s f[1]
python -V|sed 's/Python //'
python -V|tr -d 'Python '
python -V|cut -c 8-
python --version --tag-only

(pyfil via pip install pyfil.)

6 Likes

What a cute toy :slight_smile: I had no idea that something like this exists.

I use this one. I find it most readable and one of the most robust ways.

This is invalid sed code.

This one is really bad. It creates a false assumption about how it works.

2 Likes

Oops, a s went missing: python -V|sed 's/Python //' works here.

I would use the original python -c ... (or just python --version) in practice. Code golf is only a hobby. :golfing_man:

And very few of these examples work correctly (or rather “as you expect”) on PyPy.

3 Likes

I don’t understand what’s going on here using Fedora Linux:

[steve ~]$ python2.7 -V | awk '{print $2}'
Python 2.7.17
[steve ~]$ python3.7 -V | awk '{print $2}'
3.7.5

:open_mouth:
Can you please send output of this command?

python2.7 -V | od -t ax1

On my Ubuntu 22.04:

~$ python3 -V | od -t ax1
0000000   P   y   t   h   o   n  sp   3   .   1   0   .   4  nl
         50  79  74  68  6f  6e  20  33  2e  31  30  2e  34  0a
0000016

What’s happening is that python 2 prints the version to stderr.

6 Likes

Thanks Guido!