How to Check Python Version

Posted 

3 min read

Python is one of the most popular programming languages in the world. It is used for developing websites, writing scripts, machine learning, analyzing data and more.

This tutorial explains how to check what version of Python is installed on your operating system using the command line. This can be useful when installing applications written in Python that require a specific version of Python.

We’ll also show you how to programmatically determine what version of Python is installed on the system where the Python script is running. For example, when writing Python scripts, you’ll need to determine whether the script supports the version of Python installed on the user’s machine.

Checking Python Version

Python is pre-installed on most Linux distributions and macOS.

To find out which version of Python is installed on your system run the python --version or python -V command:

python --version

The command will print the default Python version, in this case, that is 2.7.15. The version installed on your system may be different.

Python 2.7.15+

The default version of Python will be used by all scripts that have /usr/bin/python set as an interpreter in the script’s shebang line.

Some Linux distributions have multiple versions of Python installed at the same time. Generally, the Python 3 binary is named python3 and the Python 2 binary is named python or python2, but that may not always be the case. You can check if you have Python 3 installed by typing:

python3 --version
Python 3.6.8

While Python 2 is well-supported and active, Python 3 is considered to be the present and future of the language.

At the time of writing this article, the latest major release of the Python is version 3.7.x. Chances are that you have an older version of Python 3 installed on your system. If you want to install the latest version the procedure depends on the operating system you are running.

Programmatically Checking Python Version

Python 2 and Python 3 are fundamentally different. The code that is written in Python 2.x may not work in Python 3.x.

The sys module that is available in all Python versions, provides system-specific parameters and functions. sys.version_info allows you to determine the Python version installed on the system. It is a tuple that contains the five version numbers: major, minor, micro, releaselevel, and serial.

Let’ say you have a script that requires at least Python version 3.5 and you want to check whether the system meets requirements. You can do that by simply checking the major and minor versions:

import sys

if not sys.version_info.major == 3 and sys.version_info.minor >= 5:
    print("This script requires Python 3.5 or higher!")
    print("You are using Python {}.{}.".format(sys.version_info.major, sys.version_info.minor))
    sys.exit(1)

If you run the script using Python version less than 3.5 it will produce the following output:

This script requires Python 3.5 or higher!
You are using Python 2.7.

To write Python code that runs under both Python 3 and 2, use the future module. It allows you to run Python 3.x-compatible code under Python 2.

Conclusion

Finding out what version of Python is installed on your system is very easy, just type python --version.

Feel free to leave a comment if you have any questions.