easiest way to check python version?

D

dmitrey

hi all,
what is easiest way to check python version (to obtain values like
2.4, 2.5, 2.6, 3.0 etc) from Python env?
I don't mean "python -V" from command prompt.
Thank you in advance, D.
 
J

John Machin

hi all,
what is easiest way  to check python version (to obtain values like
2.4, 2.5, 2.6, 3.0 etc) from Python env?
I don't mean "python -V" from command prompt.

| >>> import sys
| >>> sys.version
| '2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit
(Intel)]'
| >>> sys.version_info
| (2, 6, 2, 'final', 0)
| >>>

"easiest" depends on purpose; e.g. version for display or for logging
exactly what the customer is running. version_info (or a prefix of it)
is the best for things like conditional imports etc

E.g.
py_version = sys.version_info[:2]
if py_version == (2, 3):
from sets import Set as set

Cheers,
John
 
A

A. Cavallo

A common way to do it is (it is widely accepted):

python -c 'import sys; print sys.version[:3]'

Regards,
Antonio

hi all,
what is easiest way to check python version (to obtain values like
2.4, 2.5, 2.6, 3.0 etc) from Python env?
I don't mean "python -V" from command prompt.
| >>> import sys
| >>> sys.version
|
| '2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit

(Intel)]'

| >>> sys.version_info
|
| (2, 6, 2, 'final', 0)

"easiest" depends on purpose; e.g. version for display or for logging
exactly what the customer is running. version_info (or a prefix of it)
is the best for things like conditional imports etc

E.g.
py_version = sys.version_info[:2]
if py_version == (2, 3):
from sets import Set as set

Cheers,
John
 
S

Steve Ferg

Here is what I use in easygui:
<pre>
#--------------------------------------------------
# check python version and take appropriate action
#--------------------------------------------------
"""
From the python documentation:

sys.hexversion contains the version number encoded as a single
integer. This is
guaranteed to increase with each version, including proper support for
non-
production releases. For example, to test that the Python interpreter
is at
least version 1.5.2, use:

if sys.hexversion >= 0x010502F0:
# use some advanced feature
...
else:
# use an alternative implementation or warn the user
...
"""
if sys.hexversion >= 0x020600F0: runningPython26 = True
else: runningPython26 = False

if sys.hexversion >= 0x030000F0: runningPython3 = True
else: runningPython3 = False

if runningPython3:
from tkinter import *
import tkinter.filedialog as tk_FileDialog
from io import StringIO
else:
from Tkinter import *
import tkFileDialog as tk_FileDialog
from StringIO import StringIO

</pre>

-- Steve Ferg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top