How to Check Python Version

Updated on

4 min read

Check Python Version

Python is one of the most popular programming languages in the world. It is used for web development, data analysis, scientific computing, artificial intelligence, and more. Python is known for its ease of use, readability, and the extensive range of libraries available for different tasks.

This article explains how to use the command line to check what version of Python is installed on your Linux, macOS, or Windows machine. Knowing the installed version can be helpful when deploying applications requiring a specific Python version.

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.12.2, 3 is a major version, 12 is a minor version, and 2 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.11.6, and 3.12.0 are all part of the Python 3 major version.
  • MINOR - These releases bring 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.

Python 2 has reached the end of its life and is no longer supported, meaning that security updates, bug fixes, and other improvements will no longer be provided. It is recommended that users migrate to Python 3.

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 python3 --version or python3 -V command:

python3 --version

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

Python 3.11.6

Some Linux distributions have multiple versions of Python installed at the same time. Generally, the Python 3 binary is named python or 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 2 installed by typing:

python2 --version
Python 2.7.16

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.12.x. Chances are that you have an older version of Python 3 installed on your system.

All scripts that have /usr/bin/python3 set as an interpreter in the script’s shebang line use the default version of Python.

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’s say you have a script that requires at least Python version 3.11, and you want to check whether the system meets the 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 >= 11):
    print("This script requires Python 3.11 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 a Python version less than 3.11 it will produce the following output:

This script requires Python 3.11 or higher!
You are using Python 3.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 python3 --version or python --version.

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