Python code to find the version it is running on

To check the version of Python you can use either the system parameter sys.version or sys.version_info. sys.version returns a string containing the full version number, build number, date and the compiler details. The details returned by sys.version can be used for information purpose. On the other hand, if you want to get the major, minor, micro versions and the serial number in integer format, then use the sys.version_info. Here is an example of python code to find the version it is running on.

import sys

print("\nVersion of Python installed: ")
print(sys.version)

print("\nMajor Version: ")
print(sys.version_info[0])

print("\nMinor Version: ")
print(sys.version_info[1])

print("\nMicro Version: ")
print(sys.version_info[2])

print("\nReleaselevel: ")
print(sys.version_info[3])

print("\nSerial: ")
print(sys.version_info[4])
Python code to find the version it is running on

In the above example, I have accessed the components of sys.version_info using numbers. All the components of sys.version_info can also be accessed by their name. Here is an example of accessing the version using sys.version_info by name.

import sys

print("\nMajor Version: ")
print(sys.version_info.major)

print("\nMinor Version: ")
print(sys.version_info.minor)

print("\nMicro Version: ")
print(sys.version_info.micro)

print("\nReleaselevel: ")
print(sys.version_info.releaselevel)

print("\nSerial: ")
print(sys.version_info.serial)
accessing the version using sys.version_info by name

Related Articles

Reference


Leave your thoughts...

This site uses Akismet to reduce spam. Learn how your comment data is processed.