552

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

nnnmmm
  • 6,464
  • 3
  • 24
  • 38

6 Answers6

704

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.

Thomas Owens
  • 111,344
  • 96
  • 305
  • 429
238
import platform
print(platform.python_version())

This prints something like

3.7.2

user276648
  • 5,669
  • 6
  • 58
  • 82
Bastien Léonard
  • 58,016
  • 19
  • 77
  • 94
64

Try

python --version 

or

python -V

This will return a current python version in terminal.

Alan Hensley
  • 701
  • 6
  • 9
Atul Arvind
  • 14,947
  • 6
  • 47
  • 56
  • 4
    Nice and simple! I would recommend the latter method because older version of Python do not support the "--version" flag. Plus, the less typing for me, the better. – Steve Gelman Nov 14 '14 at 14:33
  • 35
    No it should not be the answer, because Thomas specifically asked "...in the output?". He wasn't asking how to discover which version he had installed, he was asking how to include that information into a 'print' statement. – Scott Prive Dec 05 '17 at 14:33
38
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'
ideasman42
  • 35,906
  • 32
  • 172
  • 287
GhostRyder
  • 489
  • 4
  • 4
7

If you are using jupyter notebook Try:

!python --version

If you are using terminal Try:

 python --version
Kriti Pawar
  • 722
  • 5
  • 15
0

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'>
Baris Ozensel
  • 373
  • 2
  • 10