Objective

The following artcile will provide you with the information on how to check Python version on your operating system.

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. First hint about the python version your system has installed is by checking for installed python binary executable:
$ 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
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 check for python 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

$ python
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 Python Version using script

The following python script will print the python version 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 to obtain python version.

Python 2

$ python check-python-version.py 
2.7.15rc1

Python 3

$ python3 check-python-version.py 
3.6.5
Check Python Version on Ubuntu Linux System
Check Python Version on Ubuntu Linux System
ARE YOU LOOKING FOR A LINUX JOB?
Submit your RESUME or create a JOB ALERT on LinuxCareers.com job portal.
DO YOU NEED ADDITIONAL HELP?
Get extra help by visiting our LINUX FORUM or simply use comments below.

You may also be interested in:



Comments and Discussions