How to Check Python Version (Terminal & Python Script)

Learn how to check your Python version. View instructions for checking in the terminal, command prompt, and dynamically inside a Python script.

Try Code in Editor

Explanation

Verifying the active version of Python is a critical first step when troubleshooting setup issues, installing third-party libraries, or utilizing features introduced in newer releases (such as pattern matching in 3.10+). Since systems can have multiple versions of Python installed concurrently, checking the path and version helps avoid runtime conflicts.

If you are working in a terminal or command prompt, you can check the installed Python version by running `python --version` or `python3 --version`. If Python is correctly configured in your system's PATH, the terminal will print a string showing the major, minor, and patch numbers (e.g., `Python 3.11.4`).

To check the version dynamically within a running Python script, the standard library provides the `sys` module. Calling `sys.version` returns a detailed informational string about the environment, and `sys.version_info` returns a structured tuple containing the version components, allowing you to easily verify requirements programmatically.

Step-by-Step Implementation

  1. 1

    Open your command line shell and run python --version to check the shell environment configuration.

  2. 2

    Import sys in your script and read sys.version to get a detailed version string.

  3. 3

    Query the sys.version_info tuple to check the major and minor versions programmatically.

Code Example

This script demonstrates retrieving Python environment details dynamically via the built-in sys module.

check_version.py
Try in Editor
import sys

# 1. Getting the version as a detailed string
version_str = sys.version
print("Detailed Version Info:")
print(version_str.split("\n")[0]) # Print first line of details

# 2. Checking version programmatically using version_info tuple
info = sys.version_info
print(f"\nMajor: {info.major} | Minor: {info.minor} | Micro: {info.micro}")

# 3. Setting a version check boundary
if sys.version_info >= (3, 10):
    print("System matches required Python version (>= 3.10).")
else:
    print("Warning: Upgrade recommended.")
Terminal Output
Detailed Version Info:
3.11.3 (main, Apr 10 2026, 12:00:00)

Major: 3 | Minor: 11 | Micro: 3
System matches required Python version (>= 3.10).

Frequently Asked Questions

What is the difference between python and python3 in terminal commands?

In many systems, python refers to legacy Python 2 (no longer supported), while python3 refers to the modern Python 3.x environment.

How can I enforce a minimum Python version check in my script?

You can assert sys.version_info >= (3, 8) at the start of your script, raising an error if the requirements are not met.

Related How-To Guides

Recommended Python Resources

Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.