602

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

nnnmmm
  • 7,964
  • 4
  • 22
  • 41

9 Answers9

766

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
  • 114,398
  • 98
  • 311
  • 431
260
import platform
print(platform.python_version())

This prints something like

3.7.2

user276648
  • 6,018
  • 6
  • 60
  • 86
Bastien Léonard
  • 60,478
  • 20
  • 78
  • 95
67

You can retrieve the Python version using various methods, depending on your specific needs and context. Here are some different ways to print the Python version:

  1. Using the sys module:
    import sys
    
    print("Python version")
    print(sys.version)
    print("Version info.")
    print(sys.version_info)
    
  2. Using the platform module:
    import platform
    
    print(platform.python_version())
    
  3. From the command line (Terminal or Command Prompt):
    python --version
    
  4. Using the sysconfig module (for more detailed information):
    import sysconfig
    
    print(sysconfig.get_python_version())
    
  5. Using the platform module (detailed version information):
    import platform
    
    print(platform.python_build())
    
  6. In an interactive Python shell:
    >>> import sys
    >>> sys.version
    

These methods will provide you with different levels of detail about the Python version currently installed on your system.

Atul Arvind
  • 16,054
  • 6
  • 50
  • 58
  • 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
  • 47
    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
  • The command must be executed in the OS console, not in the Python console. In a fresh console it returns the version of the base conda environment, which can be different from the environment used by the IDE/script. – mins Aug 09 '23 at 17:08
43
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
  • 42,413
  • 44
  • 197
  • 320
GhostRyder
  • 539
  • 4
  • 4
9

If you are using jupyter notebook Try:

!python --version

If you are using terminal Try:

 python --version
Kriti Pawar
  • 832
  • 7
  • 15
2

For the sake of having a one-liner :

print(__import__('sys').version)
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
  • 433
  • 1
  • 3
  • 11
0

The simplest way:

python -V 

or

python --version
Zakariya
  • 461
  • 4
  • 4
-2

If you specifically want to output the python version from the program itself, you can also do this. Uses the simple python version printing method we know and love from the terminal but does it from the program itself:

import os

if __name__ == "__main__":
    os.system('python -V') # can also use python --version
Dev Arora
  • 139
  • 5
  • 1
    Recent Ubuntu versions don’t have a `python` executable, so this would fail. – bfontaine Mar 27 '23 at 09:01
  • You can have multiple Python versions - the first one on the PATH environment variable wins - On my current project print(sys.version) gives me 3.11.4 and os.system('python -V') give me 3.10.11 – Vertexwahn Aug 23 '23 at 10:25