Python is one of the most successful programming languages in the world. It is used to develop websites, write scripts, machine learning, analyze data, and many more.

This article explains how to check what Python version is installed on your operating system working the command line. This can be beneficial when installing applications that require a particular version of python.

We will also explain to you how to programmatically determine what Python version is installed on the system where the Python script is running. For example, when writing Python scripts, you will require determining whether the script supports python’s version 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 – a python, has two major versions that are not fully cooperative: 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 announcements are bringing new features and roles. 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 include various bug fixes and enhancements.

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

Checking Python Version

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

To get which version of python is fixed on your system, run the Python –version or Python -V command:

$ python --version

The order will print the default Python version, in this case, that is 2.5.15. The version installed on your system maybe another.

Output:

Python 2.7.15+

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

Some Linux distributions have various versions of python installed at the same time. The Python 3 binary is frequently named python3, and the Python 2 binary is called python or python2, but that may not always be the case.

You can check if you have Python 3 installed by typing:

$ python3 --version
Output:

Python 3.6.8

Python 2 support ended in 2020. Python 3 is the today and future of the programming language.

At the time of writing this article, the newest major release of python is version 3.8.x. The possibilities are that you can own an older version of Python 3 installed on your system.

If you want to install the most advanced python version, the method depends on your running system.

Programmatically Checking Python Version

Python 2 and Python 3 are different. The code written in Python 2.x may not work in Python 3.x, so you have to write two other codes.

The sys module that is open in all Python versions gives system-specific parameters and functions. sys.version_info enables you to discover the Python version installed on the system. It reflects a tuple that contains the five version numbers: major, minor, micro, release level, and serial.

Let’ say you have a script that needs at least Python version 3.5, and you want to tell whether the system meets requirements. You can do that by merely monitoring 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:

Output:

Python 3.5 or higher is required to run this script. 

You are using Python 2.5.

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

Conclusion: 

Using the python --version, it is very easy to find what version of python is installed on your system.

Leave a Reply

Your email address will not be published. Required fields are marked *