[Tutor] Checking for Python version

Christian Witts cwitts at compuscan.co.za
Tue Oct 6 17:08:25 CEST 2009


Todd Zullinger wrote:
> 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.
>
>   
> ------------------------------------------------------------------------
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>   
Ah, I've only been using python since 2.2 so was not aware of this.  
Thanks for the heads-up.

-- 
Kind Regards,
Christian Witts




More information about the Tutor mailing list