Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

easiest way to check python version?

17 views
Skip to first unread message

dmitrey

unread,
Jun 10, 2009, 7:01:47 AM6/10/09
to
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.

Martin P. Hellwig

unread,
Jun 10, 2009, 7:18:08 AM6/10/09
to

You don't mean:

>>> sys.version

either?

--
MPH
http://blog.dcuktec.com
'If consumed, best digested with added seasoning to own preference.'

John Machin

unread,
Jun 10, 2009, 7:25:22 AM6/10/09
to
On Jun 10, 9:01 pm, dmitrey <dmitrey.kros...@scipy.org> wrote:
> 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. Cavallo

unread,
Jun 10, 2009, 7:38:47 AM6/10/09
to pytho...@python.org
A common way to do it is (it is widely accepted):

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

Regards,
Antonio

Scott David Daniels

unread,
Jun 10, 2009, 11:09:15 AM6/10/09
to
John Machin wrote:
> On Jun 10, 9:01 pm, dmitrey <dmitrey.kros...@scipy.org> wrote:
>> 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?
...

> "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

Note also that the definition of tuple comparison help you here:

if (2, 1, 1) < sys.version_info < (2, 3):
...
elif (2, 5) <= sys.version_info <= (2, 6, 2, 'final'):
...
else:
print('Untested')

--Scott David Daniels
Scott....@Acm.Org

Steve Ferg

unread,
Jun 22, 2009, 8:50:34 AM6/22/09
to
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

0 new messages