473,387 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

check interpreter version before running script

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
8 2029
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
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
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
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
> 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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

46
by: Jon Perez | last post by:
Can one run a 1.5 .pyc file with the 2.x version interpreters and vice versa? How about running a 2.x .pyc using a 2.y interpreter?
0
by: Atul Kshirsagar | last post by:
I am embedding python in my C++ application. I am using Python *2.3.2* with a C++ extention DLL in multi-threaded environment. I am using SWIG-1.3.19 to generate C++ to Python interface. Now to...
16
by: Neil Benn | last post by:
Hello, I'm looking at a small app which would need a very quick startup time for the Python interpreter. It doesn't do much (copying and caching of files, no GUI) but I need the Python...
12
by: Anon | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello all I am a beginner teaching myself python, and I am enjoying it immensely :) As a language it is great, I real treat to study, I actually...
12
by: Rex Eastbourne | last post by:
Hi, I'm interested in running a Python interpreter in Emacs. I have Python extensions for Emacs, and my python menu lists "C-c !" as the command to run the interpreter. Yet when I run it I get...
0
by: Simon Eves | last post by:
I am trying to write a Python module to embed the functionality of Maya (the 3D modelling and animation application from Autodesk, formerly Alias) for doing scripted scene manipulation and...
5
by: Joel | last post by:
In the course of my project, I must include some custom logic and would like to integrate a small script language in my application for that purpose. In C++, I used the LUA script language and I...
16
by: lawrence k | last post by:
I've never before written a PHP script to run on my home Ubuntu machine (though I've written dozens of PHP cron jobs for my web server), so I thought I'd start off with a very simple test: ...
4
by: skip | last post by:
Is there a reliable way (this is on Solaris if that matters) to tell if I'm running in the interactive interpreter as opposed to in a script? I think examining sys.argv works, but wanted to double...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.