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 --versionorpython3 --versionto see the installed Python version. - Inside Python code: Import the
sysmodule and printsys.versionto 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
pythoncommand 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) withsys.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
| Method | Usage | Output Example |
|---|---|---|
| Command line | python --version | Python 3.11.4 |
| Command line | python3 --version | Python 3.11.4 |
| Python code | import sys; print(sys.version) | 3.11.4 (main, Jun 6 2023, ...) |
| Python code | import 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.