[python] Printing Python version in output

How can I print the version number of the current Python installation from my script?

This question is related to python

The answer is


import sys  

expanded version

sys.version_info  
sys.version_info(major=3, minor=2, micro=2, releaselevel='final', serial=0)

specific

maj_ver = sys.version_info.major  
repr(maj_ver) 
'3'  

or

print(sys.version_info.major)
'3'

or

version = ".".join(map(str, sys.version_info[:3]))
print(version)
'3.2.2'

Try

import sys
print(sys.version)

This prints the full version information string. If you only want the python version number, then Bastien LĂ©onard's solution is the best. You might want to examine the full string and see if you need it or portions of it.


Try

python --version 

or

python -V

This will return a current python version in terminal.


If you are using jupyter notebook Try:

!python --version

If you are using terminal Try:

 python --version

import platform
print(platform.python_version())

This prints something like

3.7.2