Site icon DesignLinux

How to Check Python Version

Check Python Version

Python is one of the popular programming language. It is used for machine learning, analyzing data, websites and more. Sometimes, for developing it’s required to determine version of Python whether it is supported or not. In this article we will show you how to check the version of Python is installed on your system.

Python Version

There are different versions of Python, but the two most popular ones are Python 2.7.x and Python 3.7.x.

Python uses semantic versioning. When looking at the version number, there are usually three digits to read:

MAJOR.MINOR.MICRO

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

How to Check Python Version in Linux

Currently, most modern Linux distributions and macOS comes with Python pre-installed. For Windows, you should download and install it.

To check which version is installed on your system, open terminal on your system and run the python --version or python -V command:

python --version

In output, it will print default Python version of your system. On this system it’s 3.7.8. It may be different version on your system.

For development the default version of Python will be used. Sometimes multiple Python versions are installed on a single system. If you have Python 3 installed you can check by typing:

python3 --version
Python 3.7.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.

Check Python Version using Script

Software that’s written in one version often will not work correctly in another version. When using Python, it is essential to know which version an application requires, and which version you have.

Generally, it is recommended to have the software check the version of Python before it runs to prevent crashes and incompatibilities.

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

Use the following code snippet to check for the correct version of Python:

import sys

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

When the script runs, it will first check for the version of Python and if the version is less than 3.7 it will show following output:

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

Conclusion

In this article explained how to check the Python version.

If you have any question or suggestion, please leave a comment below.

Exit mobile version