How do I detect the Python version at runtime? [duplicate] How do I detect the Python version at runtime? [duplicate] python python

How do I detect the Python version at runtime? [duplicate]


Sure, take a look at sys.version and sys.version_info.

For example, to check that you are running Python 3.x, use

import sysif sys.version_info[0] < 3:    raise Exception("Must be using Python 3")

Here, sys.version_info[0] is the major version number. sys.version_info[1] would give you the minor version number.

In Python 2.7 and later, the components of sys.version_info can also be accessed by name, so the major version number is sys.version_info.major.

See also How can I check for Python version in a program that uses new language features?


Try this code, this should work:

import platformprint(platform.python_version())


Per sys.hexversion and API and ABI Versioning:

import sysif sys.hexversion >= 0x3000000:    print('Python 3.x hexversion %s is in use.' % hex(sys.hexversion))