Skip to content

How to check Python version?

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

Or

python -V

Or 

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
How to check Python version? | Candid.Technology

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))
    sys.exit(1)

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?

Hello There!

If you like what you read, please support our publication by sharing it with your friends, family and colleagues. If you're running an Adblocker, we humbly request you to whitelist us.

Share on facebook
Share on whatsapp
Share on twitter
Share on reddit
Share on linkedin
Share on pocket
Share on pinterest
Share on telegram
Share on stumbleupon
Share on digg
Share on tumblr
Share on email
Share on skype
Share on xing
Share on vk
Share on odnoklassniki
Share on mix








>