check interpreter version before running script

F. Petitjean littlejohn.75 at news.free.fr
Tue Apr 5 09:04:37 EDT 2005


Le Tue, 05 Apr 2005 08:57:12 -0400, rbt a écrit :
> Is there a recommended or 'Best Practices' way of checking the version 
> of python before running scripts? I have scripts that use the os.walk() 
> feature (introduced in 2.3) and users running 2.2 who get errors. 
> Instead of telling them, 'Upgrade you Python Install, I'd like to use 
> sys.version or some other way of checking before running.
> 
> 
> I thought of sys.version... but getting info out of it seems awkward to 
> me. First 5 chars are '2.4.1' in string format have to split it up and 
> convert it to ints to do proper checking, etc. It doesn't seem that 
> sys.version was built with this type of usage in mind. So, what is the 
> *best* most correct way to go about this?
try:
    from os import walk as os_walk
except ImportError:
    os_walk = None
    # raise some exception  or implement a fallback solution
# use os_walk instead of os.walk
> 
> Thanks,
> 
> rbt



More information about the Python-list mailing list