How to check Python version programmatically

Jul 15, 2024#python

Python, created by Guido van Rossum and first released in 1991, has evolved significantly over the years. Python 2.x, starting in 2000, introduced many core features and was widely used until Python 2.7 in 2010. Python 3.x, launched in 2008, brought major changes to improve the language, including better Unicode support and new syntax features. Recent versions like Python 3.11 and 3.12 continue to add optimizations and modern features.

Python is not universally pre-installed on all systems, but it is pre-installed on many Linux distributions and macOS. However, Windows does not come with Python pre-installed, though it can be easily installed.

Checking on terminal

Whether the python command refers to Python 3 depends on your operating system and how Python is configured on your system. Here is a general overview:

  • On many modern Linux distributions and macOS versions, python might refer to Python 2, while python3 explicitly refers to Python 3.
  • Some newer distributions and versions might have python pointing to Python 3, especially as Python 2 is now deprecated.
  • On Windows, if you installed Python 3 from the official installer and checked the option to add Python to PATH, the python command will typically refer to Python 3.

To check what version the python command points to, you can run:

python --version

To explicitly use Python 3, you can use:

python3 --version

If you want to configure your system so that python refers to Python 3, you can create an alias. For example, on Linux or macOS, you can add this line to your .bashrc or .zshrc file:

alias python=python3

It’s essential to understand that aliasing python to python3 means that whenever you type python in your terminal, it will actually execute python3. This could potentially affect scripts or tools that rely on specific Python versions.

Avoid making system-wide changes to the python alias unless you’re certain all users and scripts on the system are compatible with Python 3.

Checking programmatically

The sys module in Python provides access to some variables and functions that interact with the interpreter.

import sys

print(sys.version)
# 3.12.3 (main, Apr  9 2024, 08:09:14) [Clang 15.0.0 (clang-1500.3.9.4)]

print(sys.version_info)
# sys.version_info(major=3, minor=12, micro=3, releaselevel='final', serial=0)

Here sys.version is a string that contains information about the Python version currently being used, along with additional details like the build number and compiler. While sys.version_info is a tuple that provides a more structured way to access the version information. It contains five components: major, minor, micro, release level, and serial.

import sys

def check_python_version():
  """Checks the Python version and prints a message."""
  print(f"Python version: {sys.version}")

  if sys.version_info >= (3, 6):
    print("You are using Python 3.6 or later")
    # Perform actions for Python 3.6+
  else:
    print("You are using Python version lower than 3.6")
    # Perform actions for older Python versions

check_python_version()

# Output:
# Python version: 3.12.3 (main, Apr  9 2024, 08:09:14) [Clang 15.0.0 (clang-1500.3.9.4)]
# You are using Python 3.6 or later