How to Check the Version of the Interpreter in Python

Created
Modified

Using sys.version String

A string containing the version number of the Python interpreter plus additional information on the build number and compiler used. For example,

#!/usr/bin/python3

# Import module
import sys

print(sys.version)
3.10.4 (main, Apr 26 2022, 18:08:47) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]

Using sys.version_info Tuple

A tuple containing the five components of the version number: major, minor, micro, releaselevel, and serial. For example,

#!/usr/bin/python3

# Import module
import sys

print(sys.version_info)

if sys.version_info >= (3, 9):
  pass
sys.version_info(major=3, minor=10, micro=4, releaselevel='final', serial=0)

Using sys.hexversion Number

The version number encoded as a single integer. For example,

#!/usr/bin/python3

# Import module
import sys

print(sys.hexversion)
50988272

Related Tags