tags:

views:

782

answers:

3

in a python program i want to print python version (lets say 2.5.2 or 2.4) in the output. means system has python version lets say 2.5.2. i have written a program in python, now i want in the output. it should print python version. what is the code for this.

+7  A: 

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
`print (sys.version)` is better as it won't be broken by Python 3.
ilya n.
That's a good point. Updated.
Thomas Owens
+1  A: 
import platform
print(platform.python_version())

or

import sys
print("The Python version is %s.%s.%s" % sys.version_info[:3])

Edit: I had totally missed the platform.python_version() medthod.

Lennart Regebro
+5  A: 
>>> import platform
>>> platform.python_version()
'2.6.2'
Bastien Léonard