[Tutor] Checking for Python version

Todd Zullinger tmz at pobox.com
Tue Oct 6 17:02:22 CEST 2009


Christian Witts wrote:
> Your version will fail if the person is running Python 3.0, 3.1 up
> until the 3.3 series which is not good.  Neater looking (imo) code
> below.
>
> from sys import version_info, exit
>
> if version_info[0] == 1 or (version_info[0] == 2 and version_info[1] < 4):
>    exit("Please upgrade to Python 2.4 or greater.")

This would fail on python < 2.0, as version_info is not available.  So
you'd want to catch that, if you want to gracefully handle ancient
versions of python.  You could also just compare the version_info
tuple.

This is a bit ugly, but it works at least back to 1.5.2:

import sys

min_version = '2.4'
upgrade_msg = 'Please upgrade to Python %s or greater' % min_version

try:
    min = tuple(map(int, min_version.split('.')))
    ver = sys.version_info[:3]
    if ver < min:
        sys.exit(upgrade_msg)
except AttributeError:
    sys.exit(upgrade_msg)

I don't have any python 3.x systems to test though.

-- 
Todd        OpenPGP -> KeyID: 0xBEAF0CE3 | URL: www.pobox.com/~tmz/pgp
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Suppose I were a member of Congress, and suppose I were an idiot. But,
I repeat myself.
    -- Mark Twain

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 542 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/tutor/attachments/20091006/e2e9f8cd/attachment-0001.pgp>


More information about the Tutor mailing list