How to check Python version programmatically?


Python Version

To get version of the current Python running in your system, we can use sys module. import sys module and read the sys.version property.

The sys.version property returns a string value with the version, and addition information like build version, and compiler used.

We can also read sys.version_info property to get a tuple containing the version's major, minor, micro, release level, and serial.

Program

In the following program, we get the version of current Python, and print it to console.

Python Program

import sys

version = sys.version
print(version)

Output

3.10.8 (tags/v3.10.8:aaaf517, Oct 11 2022, 16:50:30) [MSC v.1933 64 bit (AMD64)]       

Now, let us use sys.version_info to get the version of Python we are using.

Python Program

import sys

version = sys.version_info
print(version)

Output

sys.version_info(major=3, minor=10, micro=8, releaselevel='final', serial=0)

Summary

In this tutorial of Python Examples, we learned how to get the version of the current Python running in your system, with examples.