Re: [Tutor] Checking for Python version
On Tue, Oct 6, 2009 at 9:59 AM, Didar Hossain didar.hoss...@gmail.com wrote: Hi, I am using the following code to check for the Python version - import os t = os.sys.version_info[0:2] if (t[0] + t[1]) 6: Hmm, what would this give for Python 1.5? How about if t (2, 4): os.sys.exit(Need at least Python 2.4) del t This snippet is put at the beginning of the single script file before the rest of the code. I need to check for the minimum specific version because I am using the @staticmethod directive. You could check for what you actually need rather than checking the version: try: staticmethod except NameError: os.sys.exit(Need at least Python 2.4) Kent ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Checking for Python version
I like Kent's try method to explicitly look for the staticmethod call - it is Pythony :-) Todd's graceful handling idea is a good one - will keep that for future use. Christian's input about my kludge failing with the 3.x series is a good catch, I didn't think about that. Thanks to all of you for the other observations. Regards, Didar ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Checking for Python version
Didar Hossain wrote: Hi, I am using the following code to check for the Python version - import os t = os.sys.version_info[0:2] if (t[0] + t[1]) 6: os.sys.exit(Need at least Python 2.4) del t This snippet is put at the beginning of the single script file before the rest of the code. I need to check for the minimum specific version because I am using the @staticmethod directive. Is there a prettier way or is this fine? Regards, Didar ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor 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.) -- Kind Regards, Christian Witts ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Checking for Python version
On Tue, Oct 6, 2009 at 9:26 AM, Christian Witts cwi...@compuscan.co.zawrote: Didar Hossain wrote: Hi, I am using the following code to check for the Python version - import os t = os.sys.version_info[0:2] if (t[0] + t[1]) 6: os.sys.exit(Need at least Python 2.4) del t This snippet is put at the beginning of the single script file before the rest of the code. I need to check for the minimum specific version because I am using the @staticmethod directive. Is there a prettier way or is this fine? Is there anything wrong with using this? import sys if sys.version '2.4': sys.exit(Need at least Python 2.4) AFAIK the string comparison is reliable -Wayne ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Checking for Python version
On Tue, Oct 6, 2009 at 9:46 AM, Steve Willoughby st...@alchemy.com wrote: On Tue, Oct 06, 2009 at 09:42:04AM -0500, Wayne wrote: On Tue, Oct 6, 2009 at 9:26 AM, Christian Witts cwi...@compuscan.co.za wrote: if sys.version '2.4': sys.exit(Need at least Python 2.4) AFAIK the string comparison is reliable Not quite. What happens when you compare '2.4' and '2.10'? '2.4' '2.10' True -- Steve Willoughby| Using billion-dollar satellites st...@alchemy.com | to hunt for Tupperware. -- To be considered stupid and to be told so is more painful than being called gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness, every vice, has found its defenders, its rhetoric, its ennoblement and exaltation, but stupidity hasn’t. - Primo Levi ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Checking for Python version
On Tue, Oct 06, 2009 at 09:47:43AM -0500, Wayne wrote: On Tue, Oct 6, 2009 at 9:46 AM, Steve Willoughby st...@alchemy.com wrote: On Tue, Oct 06, 2009 at 09:42:04AM -0500, Wayne wrote: On Tue, Oct 6, 2009 at 9:26 AM, Christian Witts cwi...@compuscan.co.za wrote: if sys.version '2.4': sys.exit(Need at least Python 2.4) AFAIK the string comparison is reliable Not quite. What happens when you compare '2.4' and '2.10'? '2.4' '2.10' True Exactly. So it doesn't work. In typical software release numbering, 2.10 is the tenth minor version of the 2.x major version series, so 2.10 is later than 2.4, but comparing as ASCII strings, it comes out the other way around. Also note that '10.1' '5.7' -- Steve Willoughby| Using billion-dollar satellites st...@alchemy.com | to hunt for Tupperware. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Checking for Python version
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. -- ToddOpenPGP - 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 pgpHQ1caffg6U.pgp Description: PGP signature ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Checking for Python version
On Tue, Oct 6, 2009 at 9:57 AM, Steve Willoughby st...@alchemy.com wrote: On Tue, Oct 06, 2009 at 09:47:43AM -0500, Wayne wrote: On Tue, Oct 6, 2009 at 9:46 AM, Steve Willoughby st...@alchemy.com wrote: On Tue, Oct 06, 2009 at 09:42:04AM -0500, Wayne wrote: On Tue, Oct 6, 2009 at 9:26 AM, Christian Witts cwi...@compuscan.co.za wrote: if sys.version '2.4': sys.exit(Need at least Python 2.4) AFAIK the string comparison is reliable Not quite. What happens when you compare '2.4' and '2.10'? '2.4' '2.10' True Exactly. So it doesn't work. In typical software release numbering, 2.10 is the tenth minor version of the 2.x major version series, so 2.10 is later than 2.4, but comparing as ASCII strings, it comes out the other way around. Also note that '10.1' '5.7' Ah... I see now. A tuple comparison would work against the version though, correct? import sys sys.version_info (2, 6, 2, 'final', 0) sys.version_info (2, 4, 0) True sys.version_info (2, 10, 0) False sys.version_info (1, 10, 0) True Which looks a lot cleaner (at least to me) than the OP... and has the advantage of working for all versions ;) -Wayne ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Checking for Python version
Didar Hossain wrote: Hi, I am using the following code to check for the Python version - import os t = os.sys.version_info[0:2] if (t[0] + t[1]) 6: os.sys.exit(Need at least Python 2.4) del t This snippet is put at the beginning of the single script file before the rest of the code. I need to check for the minimum specific version because I am using the @staticmethod directive. Is there a prettier way or is this fine? Regards, Didar How about if sys.version_info (2,4): Incidentally, your particular approach has the problem of failing for 3.1. Perhaps that was intended, but it wasn't obvious. DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Checking for Python version
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@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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Checking for Python version
On Tue, Oct 6, 2009 at 4:47 PM, Wayne sri...@gmail.com wrote: On Tue, Oct 6, 2009 at 9:46 AM, Steve Willoughby st...@alchemy.comwrote: On Tue, Oct 06, 2009 at 09:42:04AM -0500, Wayne wrote: On Tue, Oct 6, 2009 at 9:26 AM, Christian Witts cwi...@compuscan.co.za wrote: if sys.version '2.4': sys.exit(Need at least Python 2.4) AFAIK the string comparison is reliable Not quite. What happens when you compare '2.4' and '2.10'? '2.4' '2.10' True That was his point, 2.4 should be a lesser version than 2.10, so it should not ask you to upgrade if you have 2.10, but this code will. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor