Printing Python version in output

Python

Python Problem Overview


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

Python Solutions


Solution 1 - Python

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.

Solution 2 - Python

import platform
print(platform.python_version())

This prints something like > 3.7.2

Solution 3 - Python

Try

python --version 

or

python -V

This will return a current python version in terminal.

Solution 4 - Python

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'

Solution 5 - Python

If you are using jupyter notebook Try:

!python --version

If you are using terminal Try:

 python --version

Solution 6 - Python

If you would like to have tuple type of the version, you can use the following:

import platform
print(platform.python_version_tuple())

print(type(platform.python_version_tuple()))
# <class 'tuple'>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionalexView Question on Stackoverflow
Solution 1 - PythonThomas OwensView Answer on Stackoverflow
Solution 2 - PythonBastien LéonardView Answer on Stackoverflow
Solution 3 - PythonAtul ArvindView Answer on Stackoverflow
Solution 4 - PythonGhostRyderView Answer on Stackoverflow
Solution 5 - PythonKriti PawarView Answer on Stackoverflow
Solution 6 - PythonBaris OzenselView Answer on Stackoverflow