How to Check Python Version β A Quick Guide
1. Check Python Version from the Command Line
Python provides a built-in command to check its version.
β Step 1: Open the Command Line
- Windows: Press
Win + R
, typecmd
, and press Enter. - macOS/Linux: Open the Terminal (
Ctrl + Alt + T
on Linux).
β Step 2: Run the Python Version Command
For Python 3 (recommended)
python3 --version
or
python3 -V
For Python 2 (if installed)
python --version
π Example Output:
Python 3.10.12
π‘ Best Practice: Use python3 --version
to ensure you’re checking Python 3, as some systems may default to Python 2.
2. Check Python Version Inside a Script
You can also check the Python version inside a script using the sys
module.
β
Step 1: Create a Python Script (check_version.py
)
Β
import sys
print("Python Version:", sys.version)
print("Version Info:", sys.version_info)
β Step 2: Run the Script
python check_version.py
π Example Output:
Python Version: 3.10.12 (main, Dec 25 2023, 10:05:17) [GCC 9.4.0]
Version Info: sys.version_info(major=3, minor=10, micro=12, releaselevel='final', serial=0)
π‘ Best Practice: Use sys.version_info
for detailed version checks in scripts.
3. Check Python Version in VS Code & PyCharm
β VS Code
- Open the Terminal (
Ctrl + ~
). - Run
python --version
orΒ python3 –version
β PyCharm
- Open PyCharm and go to
File > Settings > Project: YourProject > Python Interpreter
. - The Python version will be displayed next to the selected interpreter.
π‘ Best Practice: In VS Code, use the Python: Select Interpreter command to check and change the Python version.