Select Page

How to Check Python Version for Linux, Mac, and Windows

by | Programming, Python, Tips

blog banner check python version

Python is a popular programming language, widely used for data science and machine learning. The language comes with a wide range of libraries and packages, which have their versions and may require a specific version of Python. This article describes how to check and get the Python version installed on your machine, whether you are using a Linux, macOS or Windows operating system.

Check the Python Version on the Command Line: –version, -V, -VV

Get Terminal with Windows

  • Press Win+R
  • Type powershell
  • Press OK or Enter

Get Terminal with macOS

  • Go to Finder
  • Click on Applications
  • Choose Utilities -> Terminal

Get Terminal with Linux

  • Press Ctrl-Alt-T or Ctrl-Alt-F2

For any of the operating systems considered you can type python –version or python -V on the command line then press Enter.

python --version
Python 3.8.8
python -V
Python 3.8.8

If you have Python2.x and Python3.x installed, you the python command is assigned to Python2.x and python3 is assigned to the Python3.x version. However, Python 2 support ended in 2020.

You are able to use the -VV option since Python 3.6, it provides more detailed information including the Clang version. Clang is a front end compiler used to compile the programming languages C, C++, and Objective C. There are Python bindings for Clang to parse C/C++ code.

python -VV
Python 3.8.8 (default, Apr 13 2021, 12:59:45) 
[Clang 10.0.0 ]

Check the Python Version in the Code Using sys, platform

You can use the standard library sys or platform to get the version of Python that is running when executing your script. The same script can be used for Windows, Mac, and Linux distributions. Checking the version within your script is useful to ensure you know what is specifically used. Even if you think Python3 is running, it may in fact be Python2, which can have downstream effects on your code.

Python Versioning

Production-ready Python uses semantic versioning in the following scheme:

MAJOR.MINOR.MICRO

Let’s take Python 3.8.8 as an example, 3 is a major version, 8 is a minor version, and 8 is a micro version

  • MAJOR – Python has two major versions that are not fully compaitble: Python 2 and Python. For example, 3.6.8, 3.7.2, and 3.8.0 are all part of the 3 major version.
  • MINOR – These releases contain new features and functions. For example, 3.6.6, 3.6.7,, 3.6.8 are all part of the Python 3.6 minor version.
  • MICRO – Micro versions contain various bug fixes and improvemnts.

For more information on development releases, you can read the Python “Development Cycle” documentation.

Using sys

Let us look at using the sys module to find out what version of Python we’re using programatically.

import sys

print(sys.version)
3.8.8 (default, Apr 13 2021, 12:59:45) 
[Clang 10.0.0 ]

We can increase the granularity of the version information using major, minor, micro, releaselevel and serial. Most of the time, you will only care about the major, minor and micro releases. Release level and serial are typically for the core Python development team to work on changes to the language. The possible release levels are alpha, beta, candidate, or final. Alpha contains the first updates made to the language. Beta means the version is testable but not working perfectly. A candidate has only a few small bugs left to fix, and final is the last version that is released to the general public. Serial is for the smallest changes. The Python Dev team increases the value as they make changes to the alpha, beta, and candidate versions. All final versions have serial = 0.

import sys

print(sys.version_info)
sys.version_info(major=3, minor=8, micro=8, releaselevel='final', serial=0)

sys.version_info is a tuple containing the five components of the version number. The releaselevel is a str and the other elements are int. Each value can be obtained by specifying an index:

print(sys.version_info[0])
3

or by specifying the index of the tuple:

print(sys.version_info.major)
3

You can use sys.version_info to ensure you are using at least a specific version of Python. In the example below, we use a minimum requirement of Python 3.5, and check using the major and minor versions:

if not (sys.version_info.major == 3 and sys.version_info.minor >= 5):
   print("This script requires Python 3.5 or higher!")
   print("You are using Python {}.{}.".format(sys.version_info.major,    sys.version_info.minor))
   sys.exit(1)
else:
<meta charset="utf-8">print("Good to go! You are using Python {}.{}.".format(sys.version_info.major,    sys.version_info.minor))
Good to go! You are using Python 3.8.

It is very easy to get your Python version. Simplicity of use is one of the main reasons why Python is very popular as a programming language. To write Python code that runs under both Python 3 and 2, use the future module, which allows you to run Python 3.x-compatible code under Python 2.

Using platform

Similar to sys.version(), platform.python_version() returns a string major.minor.patchlevel:

import platform

print(platform.python_version())

print(type(platform.python_version())
3.8.8
<class 'str'>

Using platform is particularly useful when you want the version number as a simple string.

The platform.python_version_tuple() returns a tuple, but containing major, minor, and patchlevel. The type of each element is string.

print(platform.python_version_tuple())
('3', '8', '8')

Python 2 or Python 3?

Any bugs or security problems in Python 2 are no longer being addressed since January 1st 2020. Python 3 has distinct syntax and behavior from Python 2, and is considered simpler to understand and implement. As Python 2 is no longer supported, always opt for Python 3.

Summary

You can check the Python version by typing python –version in your operating system shell or the command line in your terminal. To get more information about your environment you can use sys.version, sys.version_info, platform.python_version, and platform.python_version_tuple(). Using the programmatic version functions are useful for ensuring your code is running on the version of Python you need it to. All development should be done in Python 3 as it is the only supported major version.

Where to Go From Here?

Thank you for reading to the end of this article. I’m sure if you are just starting out with PYthon you will have many questions and would like to know how to progress. If you are data science and machine learning focused, I outline the 12 best Python libraries for data science and machine learning in this article. You can find courses for Python, specifically for data scientists in the online courses section.

Have fun and happy researching!