Python program to print the current installed version

Introduction :

In almost all programming languages, sometimes, you might have to check the current installed version. For example, if you are using one python method that is available only to a specific version and above, it is a good practice to add one fallback option, i.e. check if the current installed python version is equal or greater to that required version or not.

Python only provides a tuple containing the current version info. This is available in inbuilt sys library package. In this blog post, I will show you how to print that version info and the details of what it holds.

Get the version information in Python :

All version information is provided by the sys inbuilt library. Following are the two properties that provides these info :

1. sys.version :

This value is a string. It contains the full version information of the python interpreter. It also includes additional information like build number, compiler used etc. To get the version numbers, it is preferred to use versioninfo_ constant as explained below.

2. sys.version_info :

This is a tuple. It contains different version information like major, minor, micro, releaselevel and serial.

Here, releaselevel is string. It can be alpha, beta, candidate or final. Since this is a tuple, we can get these info using index like sys.versioninfo[0]_ for major etc.

Example :

import sys
print("major : {}".format(sys.version_info[0]))
print("minor : {}".format(sys.version_info[1]))
print("micro : {}".format(sys.version_info[2]))
print("releaselevel : {}".format(sys.version_info[3]))
print("serial : {}".format(sys.version_info[4]))

Python sys version info

Similar tutorials :