Python

How to Check Python Version on Windows/Linux/Mac

By ayed_amira , on 05/26/2020 , updated on 09/10/2020 - 8 minutes to read
check python version

How to Check Python Version : In this tutorial we will see how to get the version of python that is installed in our operating system via command prompts or directly from a script.

Overview

Python is the most widely used language in the world today. It is mainly used to develop applications, websites or in data analysis and machine learning. With these different applications, it is often necessary to install a specific version of python so that the libraries used can work correctly. So we will see how to determine the version of python installed on the OS.

Prerequisites

To get the python version, we need access to a terminal. To do this here is how to do it on each operating system :

  • Windows:  Win+R -> type powershell -> press Enter
  • Linux:  Ctrl+Alt+T, Ctrl+Alt+F2
  • MacOS:  Finder -> Applications -> Utilities -> Terminal

Python Versioning

The versions comply with a very specific standard. Python production-ready releases are versioned in the following syntax:

MAJOR.MINOR.MICRO

  • MAJOR: Python currently has two major versions that are not fully compatible with each other. These are versions 2 and 3
  • MINOR: These versions provide new functionalities to the main version (for example version 3.7 has seen the arrival of an API C called Thread Local Storage for the local storage of threads).
  • MICRO: The micro versions contain bug fixes and slight improvements.

So version 3.7.2 has 3 as Major version,7 as Minor version and 2 as Micro version.

Check Python Version Windows

By default on windows, python is not installed on the operating system so you must first install python yourself. Once you have installed python on your machine, open Powershell and do the following command:

python ––version
 

If you have installed python correctly you will get the following result:

Python 3.7.2

Note: You can also use the python command -V or python -VV since python version 3.6 which is the same as –version

You can also type directly in the windows search bar if you want to get only the major and minor version of python like this:

check python version windows

As you can see I have version 3.7 installed on my Windows computer.

Check Python Version Linux

On most linux distributions (like Ubuntu, Debian) python is installed by default on the system. To check the install version use the following command:

python ––version
 

The result is as follows:

Python 3.7.2

Check Python Version MacOS

In general, python is already installed on MacOS. To get the python version, type the following command:

python ––version
 

Python 3.7.2

Check the Python version in the script

Instead of using the command line in a terminal, it may be necessary to fetch the python version from a script. There are two python modules available to get the current version of python:

  • the sys module
  • the platform module

The advantage is that these modules are available on any operating system so you will be able to use the codes used in this tutorial. These functions are useful if for example we want to modify the processing depending on the version of python installed.

Python version using sys.version

sys.version allows you to get the version of python mainly. It returns a string of characters:

import sys

print(sys.version)
 

Output:

3.7.6 (tags/v3.7.6:43364a7ae0, Dec 19 2019, 00:42:30) [MSC v.1916 64 bit (AMD64)]

As you can see, the installed version is 3.7.6

Check Python version using sys.version_info

the sys module provides a second way to get the python version with sys.version_info. The main advantage of this method is that it returns a tuple with MAJOR, MINOR and MICRO versions:

import sys

print(sys.version_info)
print("Major version : " + str(sys.version_info[0]))
print("Minor version : " + str(sys.version_info[1]))
print("Micro version : " + str(sys.version_info[2]))
print("Release : " + str(sys.version_info[3]))
print("Serial : " + str(sys.version_info[4]))
 

Output :

sys.version_info(major=3, minor=7, micro=6, releaselevel=’final’, serial=0)
Major version : 3
Minor version : 7
Micro version : 6
Release : final
Serial : 0

Python version using platform.python_version()

The platform.python_version() function returns a string that returns the python version. Here is the syntax :

import platform

print(platform.python_version())
 

Output:

3.7.6

Python version using platform.python_version_tuple()

This function returns a tuple in the form (Major,Minor,Micro). Each element is an integer and not a string. Here’s the syntax:

import platform

print(platform.python_version_tuple())
print("Major version : " + str(platform.python_version_tuple()[0]))
print("Minor version : " + str(platform.python_version_tuple()[1]))
print("Micro version : " + str(platform.python_version_tuple()[2]))
 

Output:

(‘3’, ‘7’, ‘6’)
Major version : 3
Minor version : 7
Micro version : 6

What are the Different Python Versions

Here is the list of the different versions released to date:

Summary

As you can see, there are several methods to retrieve the versions of python installed in your operating system. Here is a summary of the different methods available:

CommandsHowExamples
python --version or
python -V or
python -VV
Terminal3.7.6
import sys
sys.version
String‘3.7.6 (tags/v3.7.6:43364a7ae0, Dec 19 2019, 00:42:30) [MSC v.1916 64 bit (AMD64)]’
import sys
sys.version_info
Tuplesys.version_info(major=3, minor=7, micro=6, releaselevel=’final’, serial=0)
import platfom
platform.python_version()
String‘3.7.6’
import platfom
platform.python_version_tuple()
Tuple(‘3’, ‘7’, ‘6’)


If you want to learn more about python, you can read this book (As an Amazon Partner, I make a profit on qualifying purchases) :

Back to the Python Menu

ayed_amira

I'm a data scientist. Passionate about new technologies and programming I created this website mainly for people who want to learn more about data science and programming :)

Comments

Leave a comment

Your comment will be revised by the site if needed.