By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,196 Members | 1,654 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,196 IT Pros & Developers. It's quick & easy.

check interpreter version before running script

P: n/a
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
Jul 18 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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.


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
Jul 18 '05 #2

P: n/a
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

Jul 18 '05 #3

P: n/a
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
Jul 18 '05 #4

P: n/a
F. Petitjean wrote:
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


You may be interested in this...
http://www.jorendorff.com/articles/python/path/
as an alternative to the os.path functions...
Martin
Jul 18 '05 #5

P: n/a
> 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:
import sys
print sys.version_info

(2, 1, 3, 'final', 0)

Regards,
Josef

Jul 18 '05 #6

P: n/a
djo
Josef Meile wrote:
What's about:
>>> import sys
>>> print sys.version_info (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, Amsterdam
import sys
sys.version_info

Traceback (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
Jul 18 '05 #7

P: n/a
"rbt" <rb*@athop1.ath.vt.edu> 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.


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>

Jul 18 '05 #8

P: n/a
rbt
Peter Otten wrote:
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.

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.
Jul 18 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.