How to check your Python version

3 apr 2024 3 min di lettura
How to check your Python version
Indice dei contenuti

Introduction

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 wide range of libraries available for different tasks.

This article explains how to use the command line to check which version of Python is installed on your Linux, macOS, or Windows computer. Knowing the installed version can be useful when deploying applications that require a specific Python version.

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

Python version

Python uses semantic versioning. Production-ready versions have a version according to 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 major version of Python 3.
  • MINOR - These versions bring new features and functions. For example, 3.6.6, 3.6.7, and 3.6.8 are all part of the minor version of Python 3.6.
  • MICRO - The new micro versions contain various bug fixes and improvements.

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

Python 2 has reached end-of-life and is no longer supported, meaning that security updates, bug fixes, and other improvements will no longer be provided. Users are advised to migrate to Python 3.

Checking your Python version

Python comes pre-installed on most Linux and macOS distributions. On Windows you need to download and install it.

To find out what version of Python is installed on your system, run the command python3 --version or python3 -V:

python3 --version

The command will print the default Python version, in this case, which 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 called python or python3, while the Python 2 binary is called python or python2, but this 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.

As of this writing, the latest major version of Python is version 3.12.x. It is likely that you have an older version of Python 3 installed on your system.

All scripts set /usr/bin/python3 as the interpreter in the Shebang line of the script 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 using.

Programmatic version control of Python

Python 2 and Python 3 are fundamentally different. Code written in Python 2.x may not work in Python 3.x.

The sys module available in all Python versions provides system-specific parameters and functions. sys.version_info allows you to determine the version of Python installed on your system. Returns a tuple containing 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 this simply by 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 lower than 3.11, the following output will be produced:

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

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

Conclusion

Finding out which version of Python is installed on your system is very simple; just type python3 --version or python --version.

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

Buy me a coffeeBuy me a coffee

Supportaci se ti piacciono i nostri contenuti. Grazie.

Successivamente, completa il checkout per l'accesso completo a Noviello.it.
Bentornato! Accesso eseguito correttamente.
Ti sei abbonato con successo a Noviello.it.
Successo! Il tuo account è completamente attivato, ora hai accesso a tutti i contenuti.
Operazione riuscita. Le tue informazioni di fatturazione sono state aggiornate.
La tua fatturazione non è stata aggiornata.