In this tutorial, we are going to learn how to find the installed Python version on your system. It is essential to know your Python version as different versions come with different features, and sometimes the code written in one version might not work as expected in other versions due to deprecated functions or other syntax differences.
Step 1: Opening the Command Prompt or Terminal
The first step is opening the command-line interface on your computer. If you are using Windows, search for Command Prompt in the Start menu, and open it. For macOS and Linux users, open the Terminal application.
Step 2: Checking Python Version
In the command prompt or terminal, type the following command:
1 |
python --version |
You should now see the installed Python version displayed in the command prompt or terminal. The output should look like this:
Python 3.8.5
If you have multiple versions of Python installed, such as Python 2.x and Python 3.x, you might need to use the following commands to check the respective versions:
1 2 |
python2 --version python3 --version |
Step 3: Checking Python Version Within a Script
You can also determine the Python version from within a script by utilizing the sys.version_info
attribute from the built-in sys
module. Create a new Python script named check_version.py
and write the following code:
1 2 3 4 5 6 7 8 |
import sys version_info = sys.version_info major = version_info.major minor = version_info.minor micro = version_info.micro print(f'Your Python version is {major}.{minor}.{micro}') |
Run this script by executing the following command in your command prompt or terminal:
1 |
python check_version.py |
The output should display the Python version:
Your Python version is 3.8.5
Full Code
Here is the full code for the Python script that checks the installed Python version:
1 2 3 4 5 6 7 8 |
import sys version_info = sys.version_info major = version_info.major minor = version_info.minor micro = version_info.micro print(f'Your Python version is {major}.{minor}.{micro}') |
Conclusion
In this tutorial, we learned how to find the installed Python version on your system using both a command line and a Python script.
Knowing your Python version helps in understanding feature availability and compatibility with different libraries and frameworks when writing your code.
Make sure to keep your Python installation up-to-date to get the latest features and security updates.