How to Check Python Version.

How To Check Python Version.

This article 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 that require a specific version of Python.

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.

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.

 

Python Versioning

Python uses semantic versioning . Production-ready releases are versioned in the following scheme:

MAJOR.MINOR.MICRO

 

For example, in Python 3.6.8, 3 is a major version, 6 is a minor version, and 8 is a micro version.

  • MAJOR – Python has two major versions that are not fully compatible: Python 2 and Python 3. For example, 3.5.7, 3.7.2, and 3.8.0 are all part of the Python 3 major version.

  • MINOR – These releases are bringing new features and functions. For example, 3.6.6, 3.6.7, and 3.6.8 are all part of the Python 3.6 minor version.

  • MICRO – The new micro versions contain various bug fixes and improvements.

Development releases have additional qualifiers. For more information, read the Python “Development Cycle” documentation.

 

Checking Python Version

Python is pre-installed on most Linux distributions and macOS. On Windows, you have to download and install it.

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.5

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

Python 2 support ends in 2020. Python 3 is the present and future of the language.

At the time of writing this article, the latest major release of the Python is version 3.8.x. Chances are that you have an older version of Python 3 installed on your system.

If you want to install the latest version of Python, 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 returns 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.