How do you check which Python version is executing your script at runtime? | A1Lab@MZeeshanM to PythonEnglish • 4 days ago

How do you check which Python version is executing your script at runtime?

message-square
0

Inspect sys.version_info (a named tuple (major, minor, micro, releaselevel, serial)) to compare Python versions programmatically in code, or print sys.version / platform.python_version() to log a human-readable version string at runtime.

Basic Example

import platform
import sys

# 1. Recommended for version checks in code: sys.version_info (supports tuple comparison)
print(sys.version_info)        # Output: sys.version_info(major=3, minor=12, micro=4, releaselevel='final', serial=0)
print(sys.version_info.major)  # Output: 3
print(sys.version_info.minor)  # Output: 12

if sys.version_info < (3, 11):
    raise RuntimeError(f"Python 3.11+ is required, but running {platform.python_version()}")

# 2. Human-readable version string (clean "X.Y.Z" vs. full build info)
print(platform.python_version())  # Output: "3.12.4"
print(sys.version)                # Output: "3.12.4 (main, Jun  6 2024, ...) [GCC ...]"

# 3. Verify which exact interpreter executable is running the script
print(sys.executable)             # Output: "/path/to/.venv/bin/python"

Key Details & Edge Cases

  • Always compare sys.version_info tuples instead of parsing sys.version strings:
    • String comparisons fail on two-digit minor versions because "3.9" > "3.12" evaluates to True lexicographically ('9' > '1').
    • sys.version_info is a namedtuple that compares component-by-component cleanly: sys.version_info >= (3, 10) evaluates to True on Python 3.10, 3.11, 3.12, etc.
  • Guard version checks BEFORE importing newer syntax or standard-library modules:
    • If your script uses syntax introduced in a newer Python version (such as match/case from 3.10 or type statements from 3.12), Python’s parser raises SyntaxError before executing line 1 of your file. Place version guards in a lightweight entry-point launcher before importing modules that rely on modern syntax.
  • Checking interpreter implementation (CPython, PyPy, MicroPython) and 32-bit vs. 64-bit architecture:
    • Use platform.python_implementation() (or sys.implementation.name) to distinguish "CPython" from "PyPy".
    • Check sys.maxsize > 2**32 (True on 64-bit Python, False on 32-bit Python) or platform.architecture()[0].
  • Confirming the active virtual environment (sys.executable & sys.prefix):
    • When multiple Python installations exist on a machine, printing sys.executable confirms whether your script is running inside your intended .venv (/project/.venv/bin/python) or the global system interpreter (/usr/bin/python3). Checking sys.prefix != sys.base_prefix returns True whenever the script is executing inside a virtual environment (venv / virtualenv).

Comparison of Runtime Python Version & Environment Attributes

Attribute / Function Module Output Example Return Type Best Used When
sys.version_info sys (3, 12, 4, 'final', 0) namedtuple Programmatic version gating (if sys.version_info >= (3, 11):).
platform.python_version() platform "3.12.4" str Logging, telemetry, or displaying a clean "major.minor.patch" string.
sys.version sys "3.12.4 (main, ...) [Clang ...]" str Full diagnostic bug reports including build date and C compiler metadata.
sys.executable sys "/opt/venv/bin/python" str Verifying which exact Python binary / virtual environment launched the script.
platform.python_implementation() platform "CPython" / "PyPy" str Detecting alternative Python runtimes (e.g., PyPy vs. standard CPython).