0

I would like to know the version of Python that I am using to run some code in my Linux environment. Over time I have several versions installed and so I just want to check the version that runs my simple code when I run code from the terminal like:

$ ./<filename>.py

When I try the following, I get different conflicting answers

[user@localhost bin]$ /usr/bin/python -V
Python 2.7.15
[user@localhost bin]$ /usr/bin/python3 -V
Python 3.6.6
[user@localhost bin]$ python -V
Python 2.7.15

How can I find out which version is active?

3
  • You just want to find out which version the "python" command in your terminal will use? Commented Jun 30, 2019 at 20:23
  • in first line of script use shebang with full path to python ie. #!/usr/bin/python and set executable chmod +x script.py and Linux will use python from shebang to run this script. Commented Jun 30, 2019 at 20:31
  • to check version in script use sys.version or sys.version_info Commented Jun 30, 2019 at 20:33

3 Answers 3

3

There are several ways to detect the version of python your program is using.

From your question I get the feeling you're not understanding why $ python -V and $ /usr/bin/python3 -V are returning different versions. This is simply due to the python command being soft linked to a specific binary version (in your case, 2.7).

If you want to understand to which binary the python command on your shell is linked to:

$ readlink -f `which python`
/usr/bin/python2.7

Programmatically you can get your Python version with:

import sys

print(sys.version_info)

Which will result in the following output:

sys.version_info(major=3, minor=6, micro=8, releaselevel='final', serial=0)

You you're just wondering which version python is using when called via a shell, you can easily find out by:

$ python -V
Python 2.7.12

You can also inspect the header of your python file and check which interpreter is being used for that particular python script when you don't specify it when you're calling the script. (i.e: $ ./python_file.py)

#!/usr/bin/python2.7
Sign up to request clarification or add additional context in comments.

Comments

3

It depends.

  • If you run it directly as an executable like you did in ./<filename>.py, it will be run as an argument to the shebang (executable mentioned in the first line of your script) you've used; some pretty common ones are #!/bin/bash, #!/usr/bin/python, #!/usr/bin/env python3 etc. One edge case is that, when you run it as ./<filename>.py without specifying any shebang; in that case, the behavior is shell dependent -- some shell will execute as an argument to itself, some shell will use /bin/sh. Beware, you don't want you shell to run your python script so always explicity mention the executable to use in shebang, even for shell executables (e.g. POSIX/non-POSIX capabilities)

  • If you run the script as an argument to an executable it will be run by that irrespective of the shebang e.g. python3 ./<filename>.py, python ./<filename>.py

For the first case, when stating a shebang, I would suggest you to use env instead of using hardcoded absolute path to the executable because env will get you the full path of the executable searching in PATH, just like running something in the command line. Which means instead of

#!/usr/bin/python3

use

#!/usr/bin/env python3

The first python3 in your path will be used. This is really helpful when you're running a script in a virtual environment and you want to use the python version from current virtual env.

I would also suggest you to look into the PATH environment variable to understand how a command like python (relative path) is searched and replaced with absolute path. (This will give you the understanding of why python -V works i.e. how shell finds which python to use).

On a similar note, use type builtin of shell to find the absolute path of an exeutable by traversing PATH.

POSIX:

type python

bash/zsh has -a argument to show executables from all locations matching the name, not just the first:

type -a python

FWIW, from within the Python process, you can find the interpreter version with sys.version (returns str) or sys.version_info (returns tuple).

Comments

0

Try this code in your terminal:

python -v

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.