0
0
PythonHow-ToBeginner · 3 min read

How to Check Python Version Quickly and Easily

To check the Python version, run python --version or python3 --version in your terminal. You can also check it inside a Python script using import sys and sys.version.
📐

Syntax

There are two main ways to check the Python version:

  • Command line: Use python --version or python3 --version to see the installed Python version.
  • Inside Python code: Import the sys module and print sys.version to get the version details.
bash and python
python --version
python3 --version

# Inside Python script
import sys
print(sys.version)
💻

Example

This example shows how to print the Python version from within a Python script using the sys module.

python
import sys
print("Python version:", sys.version)
Output
Python version: 3.11.4 (main, Jun 6 2023, 15:49:08) [GCC 12.2.1 20230224]
⚠️

Common Pitfalls

Some common mistakes when checking Python version include:

  • Using python command when your system defaults to Python 2.x instead of Python 3.x.
  • Not having Python added to your system's PATH, so the command is not recognized.
  • Confusing sys.version (a string) with sys.version_info (a tuple for detailed version parts).
python
import sys

# Wrong: expecting sys.version to be a tuple
# print(sys.version[0])  # This prints first character, not major version

# Right: use sys.version_info for parts
print(f"Major version: {sys.version_info.major}")
Output
Major version: 3
📊

Quick Reference

MethodUsageOutput Example
Command linepython --versionPython 3.11.4
Command linepython3 --versionPython 3.11.4
Python codeimport sys; print(sys.version)3.11.4 (main, Jun 6 2023, ...)
Python codeimport sys; print(sys.version_info.major)3

Key Takeaways

Use python --version or python3 --version in the terminal to quickly check Python version.
Inside Python, import sys and print sys.version for full version details.
Use sys.version_info for easy access to major, minor, and micro version numbers.
Make sure your system PATH includes Python to run version commands successfully.
Be aware that python might point to Python 2 on some systems; use python3 if needed.