Check Python Version: How To
Knowing the Python version you’re working with is important for several reasons:
- Compatibility: Certain features and modules are only available in specific versions of Python.
- Package Support: Some third-party libraries require particular Python versions to function correctly.
- Syntax Changes: Newer versions of Python introduce syntax updates and language features that may not be supported in older versions.
We’ll show you how to check Python version installed on your system, how to differentiate between Python 2 and Python 3, and how to check the version programmatically within a Python script.
Table of Contents
Why is Python Version Important?
1. Compatibility with Libraries:
Many Python packages and libraries specify compatibility with certain Python versions. For example, the popular pandas library requires Python 3.7 or later for newer versions.
2. Syntax Changes and Features:
Each new release of Python introduces new features or syntax updates. For example:
- Python 3.6 introduced f-strings for easier string formatting.
- Python 3.9 introduced new syntax like type hinting and dictionary union operators.
Running code written for a newer Python version in an older one may result in syntax errors or missing feature support.
3. Python 2 vs Python 3:
Python 2 reached its end of life on January 1, 2020. Most modern development uses Python 3, but there may still be legacy systems running Python 2. It’s important to ensure you are using the correct Python version for your project.
Checking the Python Version from the Command Line
The easiest way to check your Python version is by using the command line or terminal.
Windows, macOS, and Linux
- Open a terminal or command prompt.
- Type one of the following commands depending on your system’s Python installation:
For Python 3.x:
python3 --version
or
python3 -V
For Python 2.x (if still installed):
python2 --version
or
python2 -V
Example:
$ python3 --version
Python 3.9.7
This command prints the installed version of Python. The output shows that Python 3.9.7 is installed.
Windows Alternative:
On some Windows systems, you may need to use the following command:
python --version
If your Python installation is correctly set up, this should output the version of Python installed on your system. However, python by itself may point to either Python 2 or Python 3, depending on how your system is configured.
Differentiating Between Python 2 and Python 3
As Python 2 and Python 3 have different syntax and features, it’s important to ensure you are using the correct version for your code. You can easily check which version is being used with the following commands.
For Python 3:
python3 --version
If you’re using Python 3, the output should start with “Python 3.x.x”.
For Python 2:
python2 --version
If you’re using Python 2, the output will start with “Python 2.x.x”.
Differences in Behavior:
- Print Statement: In Python 2, print is a statement:
print "Hello, World!". In Python 3, print is a function:print("Hello, World!"). - Division: In Python 2, dividing two integers may return an integer (
5 / 2gives2), whereas in Python 3, division returns a float (5 / 2gives2.5).
If you are working on an older project or need backward compatibility, ensure you are using the correct Python version.
Checking Python Version Programmatically
You can also check the Python version from within a Python script. This is useful when you want to ensure that your code is running on the required version or if you want to display the Python version as part of your program’s output.
Using sys Module:
The sys module provides a way to access the Python version from within your program.
Example:
import sys
# Print the Python version
print("Python version")
print(sys.version)
Output:
Python version
3.9.7 (default, Aug 30 2021, 00:00:00)
[Clang 10.0.0 ]
The sys.version attribute contains detailed information about the Python version, including the compiler used.
Using sys.version_info:
If you need to access specific parts of the Python version (major, minor, or micro), use the version_info attribute.
Example:
import sys
# Print Python version as a tuple
print("Python version info:", sys.version_info)
# Check if Python 3.x is being used
if sys.version_info[0] < 3:
print("This script requires Python 3.x to run.")
Output:
Python version info: sys.version_info(major=3, minor=9, micro=7, releaselevel='final', serial=0)
The sys.version_info object provides a named tuple containing the major, minor, and micro versions of Python, which can be helpful for version checks in your code.
Checking Python Version in a Virtual Environment
If you are working in a virtual environment (virtualenv or venv), it’s important to verify that the correct version of Python is being used. The steps are similar to those for a system-wide Python installation.
- Activate the virtual environment:
source venv/bin/activate # On macOS or Linux
venv\Scripts\activate # On Windows
- Check the Python version:
python --version
This ensures that your virtual environment is using the correct version of Python.
Installing Multiple Python Versions
On many systems, you may need to install multiple versions of Python (e.g., both Python 2.x and Python 3.x) for compatibility reasons. This can be done on most platforms, but managing multiple versions requires careful configuration.
Using pyenv (macOS/Linux)
pyenv is a Python version management tool that allows you to easily switch between multiple versions of Python. You can install pyenv and use it to install and manage different Python versions.
Installing pyenv:
Follow the instructions on the official pyenv GitHub page:
pyenv GitHub
Example of Installing Python Versions:
pyenv install 3.9.7
pyenv install 2.7.18
Switching Between Python Versions:
pyenv global 3.9.7 # Use Python 3.9.7 as the global version
pyenv global 2.7.18 # Switch to Python 2.7.18
You can switch between versions as needed using pyenv.
Windows: Using py Launcher
On Windows, you can use the py launcher to select the version of Python to run. The py launcher is installed with Python on Windows.
Example of Checking Version with py:
py -3 --version # Python 3 version
py -2 --version # Python 2 version (if installed)
Running Scripts with Specific Versions:
py -3 script.py # Run with Python 3
py -2 script.py # Run with Python 2
Verifying Python Version in Different IDEs
When working in an Integrated Development Environment (IDE) like PyCharm, VS Code, or Jupyter, it’s important to ensure that your IDE is using the correct Python interpreter.
PyCharm:
- Go to File > Settings (or PyCharm > Preferences on macOS).
- Select Project > Python Interpreter.
- Verify or change the Python version being used in the project.
VS Code:
- Open the Command Palette (
Ctrl+Shift+PorCmd+Shift+P). - Search for and select Python: Select Interpreter.
- Choose the correct Python version from the list.
Jupyter Notebook:
You can check the Python version used by a Jupyter Notebook kernel with this cell:
!python --version
Key Concepts Recap
- Checking Python Version: Use
python --versionorpython3 --versionin the command line to check your Python version. - Programmatically Checking Version: Use the
sysmodule within Python scripts to check the version and ensure compatibility. - Python 2 vs Python 3: Make sure you are aware of the differences between Python 2 and Python 3, as many projects now require Python 3.
- Multiple Python Versions: You can manage multiple Python versions using tools like
pyenvor thepylauncher on Windows. - IDE Configuration: Ensure your IDE is using the correct Python interpreter for your project.
Exercise:
- Python Version Check Script: Write a Python script that checks the version of Python being used and prints a message if the version is below Python 3.6.
- Command Line Check: Open a terminal and check the installed Python version. If Python 2 is also installed, verify its version.
- Virtual Environment Check: Create a virtual environment using Python 3.8 and verify the Python version within the virtual environment.
You can always check the official Python documentation to learn more. However, you might like to check out…
Our free Learn Python Programming Masterclass to hone your skills or learn from scratch.
The course covers everything from first principles to Graphical User Interfaces and Machine Learning
FAQ
Q1: How do I check my Python version in the terminal?
A1: To check your Python version in the terminal, use the following commands:
- For Python 3.x:
python3 --version
or
python3 -V
- For Python 2.x (if still installed):
python2 --version
or
python2 -V
On Windows, you might also try:
python --version
Q2: Why does python --version give me Python 2.x instead of Python 3.x?
A2: On some systems, particularly older Linux distributions and certain Windows setups, python may still point to Python 2 by default. To use Python 3, use python3 instead of python. You can also configure your system to default to Python 3 by updating system settings or using a version manager like pyenv.
Q3: How can I check the Python version inside a script?
A3: You can check the Python version programmatically within a script using the sys module.
Example:
import sys
print(sys.version)
This will output detailed information about the Python version and build. You can also use sys.version_info to get individual components (major, minor, and micro versions).
Q4: What’s the difference between Python 2 and Python 3?
A4: Python 3 introduced many improvements and changes that are not backward-compatible with Python 2. Some key differences include:
- Print function: In Python 3,
printis a function (print("Hello")), whereas in Python 2, it was a statement (print "Hello"). - Integer division: Python 2 returns an integer when dividing integers (
5 / 2 = 2), while Python 3 returns a float (5 / 2 = 2.5). - Unicode strings: In Python 3, strings are Unicode by default, while Python 2 has separate types for strings (
str) and Unicode (unicode).
Q5: How can I manage multiple versions of Python on my system?
A5: You can manage multiple Python versions using tools like:
pyenv(Linux/macOS): A Python version manager that allows you to easily switch between different Python versions.pylauncher (Windows): This allows you to specify which Python version to use. For example:
py -3 --version # Python 3 version
py -2 --version # Python 2 version (if installed)
Q6: How do I check the Python version in a virtual environment?
A6: First, activate the virtual environment:
- On macOS/Linux:
source venv/bin/activate
- On Windows:
venv\Scripts\activate
Then, check the Python version with:
python --version
This ensures you are using the correct Python version in the virtual environment.
Q7: What if my IDE (e.g., PyCharm or VS Code) is using the wrong Python version?
A7: You can change the Python interpreter in most IDEs:
- PyCharm:
Go to File > Settings > Project > Python Interpreter, and select the correct version of Python. - VS Code:
Use the Command Palette (Ctrl+Shift+PorCmd+Shift+P) and search for Python: Select Interpreter. Then, choose the correct version from the list.
Make sure you are selecting the right Python interpreter for your project.
Q8: Can I check for a minimum Python version in my script?
A8: Yes, you can check for a minimum Python version using the sys.version_info tuple. For example, to ensure the script runs only on Python 3.6 or higher:
import sys
if sys.version_info < (3, 6):
print("This script requires Python 3.6 or higher.")
sys.exit(1)
This will exit the program if the Python version is lower than 3.6.
Q9: How can I install a specific version of Python using pyenv?
A9: To install a specific version of Python using pyenv, follow these steps:
- Install
pyenv(see the official pyenv GitHub page for installation instructions). - Install the desired version:
pyenv install 3.9.7
- Set the global or local version of Python:
pyenv global 3.9.7
pyenv local 3.9.7 # For the current directory
Q10: How do I verify which Python version is being used in Jupyter Notebook?
A10: You can check the Python version inside a Jupyter Notebook by running this cell:
!python --version
Alternatively, you can use:
import sys
print(sys.version)
This will print the version of Python that Jupyter is using for the notebook kernel.
Q11: How do I install an older or specific version of Python?
A11: On most platforms, you can download older versions of Python from the official Python website (https://www.python.org/downloads/) or use a version management tool like pyenv. Follow the installation instructions for your platform.
Q12: What does python -V do, and how is it different from python --version?
A12: Both python -V and python --version provide the same output and are interchangeable. They display the current Python version installed on your system. The -V flag is just a shorter form of --version.
Q13: Why is checking the Python version important when using third-party libraries?
A13: Many third-party libraries require specific Python versions to function correctly. For example, newer versions of libraries like pandas or Django may only work on Python 3.7 or higher. By ensuring you are using a compatible Python version, you avoid import errors and compatibility issues.
Q14: How do I ensure my project uses the same Python version across different environments?
A14: To ensure consistency across environments, use virtual environments (venv or virtualenv) to isolate your project’s Python version and dependencies. You can also use pyenv to manage Python versions on different systems.
Additionally, you can specify the required Python version in your pyproject.toml or requirements.txt file (e.g., in setup.py):
python_requires='>=3.6'
Q15: How do I check if Python is correctly installed on my system?
A15: You can verify that Python is installed by running python --version or python3 --version in your terminal. If Python is correctly installed, this will return the version number.
If you get a “command not found” error, you may need to install Python or update your system’s PATH environment variable to include the Python executable.

