check interpreter version before running script

Peter Hansen peter at engcorp.com
Tue Apr 5 09:20:40 EDT 2005


rbt wrote:
> 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.
> 
> Whatever I do, I need it to work on Linux, Mac and Windows.

Why does this have to occur "before running the script"?  Can't
you just do it as the first thing the script does on startup?
That is the usual Best Practice approach.

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

Use sys.version_info instead.  As it's a tuple, you can
just slice-and-dice as needed, and compare subsets of
it with other tuples.

-Peter



More information about the Python-list mailing list