>>> import sys>>> sys.version_info(2, 6, 4, 'final', 0)>>> if not sys.version_info[:2] == (2, 6):... print "Error, I need python 2.6"... else:... from my_module import twoPointSixCode>>>
desktop:~$ pythonPython 2.7.6 (default, Jun 22 2015, 18:00:18)[GCC 4.8.2] on linux2Type "help", "copyright", "credits" or "license" for more information.>>>
via_platform = 0check_sys = 0via_sys_version_info = 0via_sys_version = 0test_sys = 0try:import platformexcept (ImportError, NameError):# We have no platform module - try to get the info via the sys modulecheck_sys = 1
if not check_sys:if hasattr(platform, "python_version"):via_platform = 1else:check_sys = 1
if check_sys:try:import systest_sys = 1except (ImportError, NameError):# just let via_sys_version_info and via_sys_version remain False - we have no sys modulepass
if test_sys:if hasattr(sys, "version_info"):via_sys_version_info = 1elif hasattr(sys, "version"):via_sys_version = 1else:# just let via_sys remain Falsepass
if via_platform:# This gives pretty good info, but is not available in older interpreters. Also, micropython has a# platform module that does not really contain anything.print(platform.python_version())elif via_sys_version_info:# This is compatible with some older interpreters, but does not give quite as much info.print("%s.%s.%s" % sys.version_info[:3])elif via_sys_version:import string# This is compatible with some older interpreters, but does not give quite as much info.verbose_version = sys.versionversion_list = string.split(verbose_version)print(version_list[0])else:print("unknown")