The following article will provide you with the information on how to check Python Version on your operating system. You will learn how to check for python version using the python command as well as how to determine the python version programmatically, from python console and using python script.
Difficulty
EASY
Conventions
- # – requires given linux commands to be executed with root privileges either directly as a root user or by use of
sudo
command - $ – requires given linux commands to be executed as a regular non-privileged user
Instructions
You system may have both Python 2 and Python 3 version installed. List installed python binary executable to see what version is installed on your system:
$ ls /usr/bin/python* /usr/bin/python /usr/bin/python2 /usr/bin/python2.7 /usr/bin/python3 /usr/bin/python3.6 /usr/bin/python3.6m /usr/bin/python3m
Please note the action minor python version may differ from system to system. Another path the Python binaries can be installed in is /usr/local/bin/
. As you can see our system has both Python 2 and Python 3 versions installed.
Check Python Version from command line
Next, we are going to retrieve the version number from the command line:
Python 2
$ /usr/bin/python -V OR /usr/bin/python --version Python 2.7.15rc1
Python 3
$ /usr/bin/python3 -V OR /usr/bin/python3 --version Python 3.6.5
Check Python Version by using interpreter
Instead of retrieving the python version from the linux command line we can ask directly the python interpreter to perform version check.
Python 2
$ python Python 2.7.15rc1 (default, Apr 15 2018, 21:51:34) [GCC 7.3.0] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import platform >>> platform.python_version() '2.7.15rc1' >>>
Python 3
$ python3 Python 3.6.5 (default, Apr 1 2018, 05:46:30) [GCC 7.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import platform >>> platform.python_version() '3.6.5' >>>
Check version using script
The following script will check python version and print the version number to the standard output. Save the below python code as a new file named check-python-version.py
:
import platform
python_version=platform.python_version()
print (python_version)
Once ready run the check-python-version.py
script to obtain python version. Make sure to use appropriate Python interpreter such as python or python3.
Python 2
$ python check-python-version.py 2.7.15rc1
Python 3
$ python3 check-python-version.py 3.6.5
{loadposition python-tutorial-toc}