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.
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
Open your command line shell and run python --version to check the shell environment configuration.
- 2
Import sys in your script and read sys.version to get a detailed version string.
- 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.
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.")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.
Python Loops
Learn how to use Python loops to iterate over data. Master for loops, while loops, break, continue, and loop best practices with interactive examples.
Python Type Conversions
Learn implicit and explicit type conversions in Python. Convert between strings, integers, floats, lists, sets, and dictionaries.
Python vs JavaScript: Which Programming Language is Best?
A comprehensive comparison between Python and JavaScript. Explore syntax differences, performance, use cases (backend vs frontend), and coding examples.