Getting the Python version

You can get the Python version with sys.version:

import sys
print(sys.version)
'3.4.3 (default, Oct 14 2015, 20:28:29) \n[GCC 4.8.4]'

However, rather than checking the version it’s much simpler and more Pythonic to try to use a version-dependent function and handle failure if it isn’t there.

Calling a top-level function that doesn’t exist will raise a NameError:

try:
    might_exist()
except NameError as err:
    # Do something else instead

While calling a module-level function that doesn’t exist will raise an AttributeError

try:
    maybe_module.might_exist()
except AttributeError as err:
    pass
    # Do something else instead