check interpreter version before running script

R

rbt

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.

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?

Thanks,

rbt
 
P

Peter Otten

rbt said:
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 like

import os

try:
os.walk
except AttributeError:
# implement fallback

No need to remember in which version the desired feature came to be.

Peter
 
F

F. Petitjean

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
 
P

Peter Hansen

rbt said:
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
 
J

Josef Meile

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.

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?
What's about:
(2, 1, 3, 'final', 0)

Regards,
Josef
 
D

djo

Josef said:
What's about:
(2, 1, 3, 'final', 0)

Python 1.5.2 (#1, Mar 3 2001, 01:35:43) [GCC 2.96 20000731 (Red
Hat Linux 7.1 2 on linux-i386
Copyright 1991-1995 Stichting Mathematisch Centrum, AmsterdamTraceback (innermost last):
File "<stdin>", line 1, in ?
AttributeError: version_info


....which the OP may have known, since they suggested sys.version,
which *is* availible in pre-2.0.


My own messy solution involves a shell script installer which
runs a 1.5-safe Python script which installs a needs-2.2 Python
script. But I needed an installer for other reasons. This would
be ugly for a single script.


djo
 
F

Fredrik Lundh

rbt said:
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.

if you depend on os.walk, check for os.walk.

try:
from os import walk
except ImportError:
print "sorry, you need a newer python version!"
sys.exit()

or

import os
try:
os.walk
except AttributeError:
print "sorry, you need a newer python version!"
sys.exit()

if you only depend on a few functions, you can usually emulate them in
earlier versions. if 2.2 or newer is a reasonable requirement, you can
put a copy of the walk function in your script:

from __future__ import generators
try:
from os import walk
except ImportError:
def walk(...):
# copied from os.py in 2.3

if you want to support e.g 1.5.2 or newer, you can use something like this:

import os
try:
from os import walk
except ImportError:
class walk:
def __init__(self, directory):
self.stack = [directory]
def __getitem__(self, index):
dirpath = self.stack.pop(0)
dirnames = []
filenames = []
for file in os.listdir(dirpath):
name = os.path.join(dirpath, file)
if os.path.isdir(name) and not os.path.islink(name):
dirnames.append(file)
self.stack.append(name)
else:
filenames.append(file)
return dirpath, dirnames, filenames

(tweak as necessary)

</F>
 
R

rbt

Peter said:
rbt wrote:




I like

import os

try:
os.walk
except AttributeError:
# implement fallback

No need to remember in which version the desired feature came to be.

Peter

Thanks for all the tips. I found this tip from Peter the best for my
situation.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top