import sys
print(sys.version)
That one-liner tells you everything about the Python you're running. Version number, build date, compiler — all of it. If you've ever stared at a cryptic error message and thought "wait, which Python am I even using?" — you need to know how to check Python version. There are a few ways to do it, and each one is useful in different situations.
From the Terminal — The Quick Check
The fastest way to check your Python version. Just open a terminal and type:
python --version
Or if you're on a system where python still points to Python 2 (looking at you, older macOS):
python3 --version
You can also use the -V flag — same thing, just shorter:
python -V
This is what I use 99% of the time. Quick, easy, done. But sometimes you need the version inside your code.
sys.version — The Full String
If you need the version as a string inside your Python script, sys.version is your friend:
import sys
print(sys.version)
print(type(sys.version))
It returns a string with the full version info including the build details. Useful for logging or diagnostics, but kind of annoying to parse if you just need the major/minor numbers.
sys.version_info — The One You Actually Want
This is the one I reach for most in actual code. It returns a named tuple you can compare directly:
import sys
print(sys.version_info)
print(sys.version_info.major)
print(sys.version_info.minor)
print(sys.version_info.micro)
The killer feature? You can compare it with tuples. This is how you do version gates in scripts:
import sys
if sys.version_info >= (3, 10):
print("You can use match/case!")
elif sys.version_info >= (3, 8):
print("You can use walrus operator :=")
else:
print("Time to upgrade, friend")
I use this pattern all the time in libraries that need to support multiple Python versions. It's clean, readable, and doesn't involve string parsing. Much better than trying to split sys.version on dots.
platform.python_version() — Clean and Simple
Just want the version number as a clean string? No build info, no noise?
import platform
print(platform.python_version())
print(platform.python_version_tuple())
The platform module also gives you a tuple version, but heads up — the tuple contains strings, not ints. So you can't do platform.python_version_tuple() >= ('3', '10') and expect it to work correctly with version numbers above 9. Stick with sys.version_info for comparisons.
Version Check in a Script — The Guard Pattern
If you're writing a script that needs a minimum Python version, put the check right at the top:
import sys
MIN_VERSION = (3, 9)
if sys.version_info < MIN_VERSION:
sys.exit(f"Python {MIN_VERSION[0]}.{MIN_VERSION[1]}+ required. You have {sys.version_info.major}.{sys.version_info.minor}")
# Rest of your script...
print("All good, running on a supported Python version")
I've seen people skip this and then get confused Stack Overflow posts when their f-strings don't work on Python 3.5. Just add the guard. It takes 3 lines and saves everyone headaches.
Python vs Node.js — How Do They Compare?
If you're coming from the Node.js world, here's the equivalent:
// Node.js version check
console.log(process.version); // v20.10.0
console.log(process.versions.node); // 20.10.0
// Version comparison
const [major, minor] = process.versions.node.split('.').map(Number);
if (major >= 18) {
console.log("LTS or newer");
}
And the Python equivalent:
import sys
print(sys.version) # 3.12.0 (...)
print(f"{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro}") # 3.12.0
if sys.version_info >= (3, 10):
print("Modern Python")
Python's sys.version_info is nicer for comparisons — no string splitting and parsing needed. Node makes you do that yourself. "Works on my machine" hits different when you realize you're on Python 3.12 and production is running 3.8.
Quick Reference
python --version— Terminal quick checksys.version— Full version string with build infosys.version_info— Named tuple, best for comparisonsplatform.python_version()— Clean version string ("3.12.0")sys.version_info >= (3, 10)— Version gate pattern
Knowing how to check your Python version sounds basic, but it saves you from so many "why doesn't this work" moments. Especially when you're juggling virtual environments, system Python, and that random Homebrew install from 2019.
Converting Python scripts to another language? CodeConverter can translate version-specific code between Python, Node.js, and 25+ other languages.