Skip to content

How to check Python version?

  • by
  • 3 min read

Python is one of the most popular and easy to learn programming languages on the planet right now. The language can be used for anything from developing websites, writing scripts, machine learning, data analysis, AI implementation, and many more. 

However, there are two major versions of Python out there. The outdated and discontinued Python2, which was the main Python branch for quite some time and the new Python3, is the newer version.

In this article, we’re looking at how to check the Python version you are running.

Also read: How to run Python in terminal?

Understanding Python versions

Before we get to check what Python version you have installed, let’s take a moment to know what Python versions mean.

Python uses semantic versioning, which means that production-ready releases are versioned as Major.Minor.Micro

For example, the latest version of Python, Python 3.9.2, released on 19 February 2021, has three digits in the version number. The denotes the major version, the the minor version and the the micro version.

Here’s what they mean.

  • Major: Major versions are the ones where the largest changes are made. Python major versions are not compatible with each other. 
  • Minor: Minor versions bring new features and functions to the language. 
  • Micro: Micro versions are often just simple bug fixes and improvements. 

You can read the Python Development Cycle documentation for additional qualifiers. 

Also read: How to split Python strings?

How to check Python version?

Checking the actual version of Python on your machine is rather simple. Just open a Python terminal and type the following command. 

python --version


python -V


python -VV

This will output the default Python version on your machine. This version is defined in the /usr/bin/python interpreter. 

If you’ve got Python2 and Python3 installed on your system simultaneously, you can check the Python3 version by typing the following command.

python3 --version

Checking Python version in scripts

Since Python2 and Python3 are two different major versions, scripts written for one version will not run on the other. It’s a good development practice to implement a small version check in your script before you start running any major operations. 

You can do this by using the sys.version_info function from the sys module. This function returns a tuple value contains the major, minor, micro, release level and serial version numbers. 

For example, if you’re writing a script that requires Python 3.7 or above to run, you can implement a check as follows.

import sys

if not (sys.version_info.major == 3 and sys.version_info.minor >= 7):
    print("This script requires Python 3.5 or higher!")
    print("You are using Python {}.{}.".format(sys.version_info.major, sys.version_info.minor))

Now, this snippet will terminate the program if the default Python version on the machine it’s running on is lower than 3.7.

Also read: How to get the current working directory in Python?

Yadullah Abidi

Yadullah is a Computer Science graduate who writes/edits/shoots/codes all things cybersecurity, gaming, and tech hardware. When he's not, he streams himself racing virtual cars. He's been writing and reporting on tech and cybersecurity with websites like Candid.Technology and MakeUseOf since 2018. You can contact him here:

Exit mobile version