Figuring out how to check the Python version on your Windows / macOS or Linux computer can be a confusing task at times.
Python is one of the most popular programming languages in the world, with it being in a wide range of scenarios — web development, machine learning, writing bots and scripts, automated trading, analyzing data, etc.
Whether you’re a beginner to Python programming or an experienced engineer, it’s almost always the case that we tend to forget certain commands which we need to do certain tasks with Python, in this case checking the Python version.
In this guide, we will show you the quickest and most reliable ways to check your Python version on your Windows, macOS or Linux-running computer with step-by-step instructions.
Let’s dive right in.
How Python Versioning Works.
It’s worth emphasizing how Python versioning works so that you are able to understand what the numbers mean when we for instance say Python version 3.7.5.
Simply put, Python uses semantic versioning which means that each version number comes with three digits with each digit giving a different meaning.
For instance, let’s take our earlier example and say we have Python 3.7.5. What does the 3, 7, and 5 mean in this case?
Based on semantic versioning, it is given that every version number is written as
MAJOR.MINOR.PATCH
For Python 3.7.5, this would mean that 3 is the major version, 7 is the minor version and 5 is the micro version.
- MAJOR – Python has two major versions that are not fully compatible: Python 2 and Python 3. For example, 3.5.7, 3.7.2, and 3.8.0 are all part of the Python 3 major version and versions 2.3.3, 2.5.6 and 2.8.0 are all part of the Python 2 major version. Notice the difference?
- MINOR – These releases are considered minor releases and are usually brought about to bring new features to the major version. For example, 3.7.3, 3.7.4, and 3.7.5 are all part of the Python 3.7 minor version.
- MICRO – The patch versions contain patches or various bug fixes and improvements.
There are a few more semantics available for development releases but we shouldn’t be concerned with those unless if you’re interested in helping develop and maintain Python.
1. Checking Python Version the Simplest Way (on All Platforms).
The easiest way to check your Python version is to simply use the following command (take note of the capital ‘V’):
python -V
The command will print the default Python version, in this case, that is 2.7.15. The version installed on your system may be different.
Python 3.8.5
This is both stated in the Python documentation as well as the most common way of finding out the Python version you have installed on your Windows, macOS or Linux machine.
You can also alternatively use the below command as well especially if you know that you’re using a Python version that is version 2.5 or greater.
python --version
If you’re using an older version of Python (particularly Python 2.4 or older), then we recommend that you try out Method 2 below to get the Python version.
2. Using sys to Check Python Version.
Both Python 2 and Python 3 are a different version altogether. This means that the code that you write or is already written in Python 2 may not work when using it within Python 3.
Using the sys module method will allow you to check your Python version, without limiting you to a particular version such as the python -V / python –version commands which we talked about above (Method 1).
The sys module is available on all Python versions and hence also makes it a great reliable method to provide you with the python version.
Here’s how you use the sys module to check the Python version:
- Firstly, open up your Command Prompt (Windows) or Terminal (macOS or Linux) and then run the following command:
python
- Next, import the sys module as shown below.
>>> import sys
- Once you’ve imported the sys module, simply enter the following command to get the Python version (take note that the use of parenthesis is important!):
>>> print(sys.version) # parentheses necessary in python 3.
3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:43:08) [MSC v.1926 32 bit (Intel)]
Tips
You can also carry out further processing to obtain more information about the Python version (as shown below).
>>> sys.version_info
sys.version_info(major=3, minor=8, micro=5, releaselevel='final', serial=0)
or
>>> sys.hexversion
50857456
That’s it!
We have also received questions from readers on how to ensure that a script that you write in Python checks and runs with the minimal version requirement. To do that, simply enter the below assert statement into your code and it will compare the major and minor versions information.
assert sys.version_info >= (2, 5)
Alternatively, you can also enter:
import sys
if not (sys.version_info.major == 3 and sys.version_info.minor >= 5):
print("Requires Python 3.5 or higher")
print("You are currently using Python {}.{}.".format(sys.version_info.major, sys.version_info.minor))
sys.exit(1)
The output will hence show if you try to run a script that is less Python 3.5 as follows:
This script requires Python 3.5 or higher!
You are using Python 2.8.
If you are intending to write Python code to support Python 2 and upwards, then we recommend that you use the future module. This will allow you to create your code that readily is compatible with both Python 2 and Python 3 version with minimal overhead. This is an excellent module that will help provide support for a wide range of Python versions.
Voila! That’s it.
3. Using platform
Similar to that of the sys module method, you can also use the platform module and use the python_version() method to output the Python version.
This also works well if you want to check the Python version in Jupyter notebook, as in some cases both the sys module method and the -V or –version methods may not work.
Here’s how you use the platform module to check the Python version:
- Firstly, open up your Command Prompt (Windows) or Terminal (macOS or Linux) and then run the following command:
python
- Next, import the python_version function from the platform (as shown below):
from platform import python_version
Note: Unlike the sys module method where you had to import the sys module at first, you do not have to download or import the platform module separately as it is available via the Python standard library (stdlib).
- Lastly, print out the Python version as shown below:
print(python_version())
Voila! You should get an output that shows the version number of the Python you have installed on your machine.
There you go. Hopefully, you’ve found the above steps useful to check Python version on your Windows, macOS or Linux computer. If you have any questions feels free to let us know down below and we will try our best to assist. 🙂
Happy coding!