≡ Menu

Find version of python your script is running on

Sometimes your python scripts requires minimum version of python as a few things keeps changing from version to version. If your script is using any of such facilities it is important to ensure that the python version you are using is exactly same(or minimum version).

There are many ways to find this information. However, I will show you the basic one.

import sys

version = sys.version_info
print("Major version is : {0}".format(version.major))
print("Minor version is : {0}".format(version.minor))

Executing the above code will show the version number of python using which the script is running. For demonstration purpose, I have copied the above code into a file called test_pyversion.py file and executed with different versions of python.

In above screen my default version of python is 2.7 and python3 is pointing to python 3.5. When I ran the script using python 2.7 and python 3.5, it displayed the version numbers.

You can use a simple if condition inside the script to check the required version number and exit it if required version not found.

Hope you will find this useful.

{ 0 comments… add one }

Leave a Comment