Objective
The following artcile will provide you with the information on how to check Python version on your operating system.Difficulty
EASYConventions
- # - 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/python3mAnother 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
Subscribe to RSS and NEWSLETTER and receive latest Linux news, jobs, career advice and tutorials.
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 namedcheck-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
- Python Introduction and Installation Guide
- Python Files and the Interpreter
- Experimenting With Numbers and Text In Python
- Python Variables
- Working With Number Variables In Python
- Python String Basics
- Advanced Python Strings
- Python Comments
- Python Lists
- Python List Methods
- Python Multidimensional Lists
- Python Tuples
- Python Boolean Operators
- Python If Statements
- Python While Loops
- Python For Loops
- Python Dictionaries
- Python Advanced Dictionaries
- Python Functions