Skip to main content
Basics

How to check the Python version

Quick answer

In the shell: python --version. In code: import sys; print(sys.version_info).

import sys
print(sys.version)         # '3.13.0 (main, Oct  7 2024, ...)'
print(sys.version_info)    # sys.version_info(major=3, minor=13, micro=0, ...)

# Programmatic check for a minimum version:
if sys.version_info < (3, 10):
    raise RuntimeError("Python 3.10 or newer required")

sys.version is the human-readable string; sys.version_info is a named tuple you can compare with tuple math (< 3.10 works). Use version_info for programmatic checks β€” parsing sys.version with regex is fragile.

In a shell, `python --version` prints the version. On systems with both Python 2 and 3 installed, use `python3 --version` to force Python 3. Modern macOS ships without a `python` command at all β€” only `python3`.

Inside a venv, `python --version` reports the venv's Python (which is what you almost always care about). Outside the venv, it reports the system default.

Variations

Check on the command line

# python --version
# python -V
# python3 --version   # if 'python' is ambiguous

-V is the short form.

Compare specific components

if sys.version_info.major == 3 and sys.version_info.minor >= 12:
    from datetime import UTC   # 3.11+

For feature-gating imports.

Full version + build info

print(sys.version)
print(sys.platform)      # 'linux' / 'darwin' / 'win32'
print(sys.executable)    # full path to the interpreter

Full diagnostic when troubleshooting environment issues.

Practise this in your browser

Every how-to has a live lesson. Free tier, no card.

Start free β†’

Unlock every lesson

Pro β€” $12/mo, unlimited AI, cancel any time.

See Pro plans β†’

Get one Python lesson + one career idea every Friday

No spam, no "buy our course now". Three bullets, every Friday. Unsubscribe with one click.

Related how-tos