How do I check what version of Python is running my script?



Python version is displayed on console immediately as you start interpreter from command line

C:\Users\acer>python
Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

Version information is present in version attribute defined in sys module

>>> import sys
>>> sys.version
'3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit (AMD64)]'

Another attribute version_info is more elaborate. It provides major, minor and micro version levels.

>>> import sys
>>> sys.version_info
sys.version_info(major=3, minor=6, micro=1, releaselevel='final', serial=0)

There is also hexversion attribute which provides a unique number associated with the version. If converted using hex() function, it indicates the version.

>>> sys.hexversion
50725360
>>> hex(50725360)
'0x30601f0'


Advertisements