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_infotuples instead of parsingsys.versionstrings:- String comparisons fail on two-digit minor versions because
"3.9" > "3.12"evaluates toTruelexicographically ('9' > '1'). sys.version_infois anamedtuplethat compares component-by-component cleanly:sys.version_info >= (3, 10)evaluates toTrueon Python 3.10, 3.11, 3.12, etc.
- String comparisons fail on two-digit minor versions because
- 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/casefrom 3.10 ortypestatements from 3.12), Python’s parser raisesSyntaxErrorbefore executing line 1 of your file. Place version guards in a lightweight entry-point launcher before importing modules that rely on modern syntax.
- If your script uses syntax introduced in a newer Python version (such as
- Checking interpreter implementation (
CPython,PyPy,MicroPython) and 32-bit vs. 64-bit architecture:- Use
platform.python_implementation()(orsys.implementation.name) to distinguish"CPython"from"PyPy". - Check
sys.maxsize > 2**32(Trueon 64-bit Python,Falseon 32-bit Python) orplatform.architecture()[0].
- Use
- Confirming the active virtual environment (
sys.executable&sys.prefix):- When multiple Python installations exist on a machine, printing
sys.executableconfirms whether your script is running inside your intended.venv(/project/.venv/bin/python) or the global system interpreter (/usr/bin/python3). Checkingsys.prefix != sys.base_prefixreturnsTruewhenever the script is executing inside a virtual environment (venv/virtualenv).
- When multiple Python installations exist on a machine, printing
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). |
You must log in or register to comment.
