I have a Python file which might have to support Python versions < 3.x and >= 3.x. Is there a way to introspect the Python runtime to know the version which it is running (for example, 2.6 or 3.2.x)?
I have a Python file which might have to support Python versions < 3.x
Share
Sure, take a look at
sys.versionandsys.version_info.For example, to check that you are running Python 3.x, use
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_infocan also be accessed by name, so the major version number issys.version_info.major.See also How can I check for Python version in a program that uses new language features?