/usr/bin/env python, force a version

M

manatlan

I've got a trouble, and i think that anybody there can help me

I've got a python script which i distribute in somes packages for *nix.
This script is full of python and need python 2.4 ! And i'd like to
display a message when the user doesn't have a python2.4 version.

if i put, at the top of the script (which can be done with /usr/bin env
too):

#/usr/bin/python2.4
it will be good, but py2.3 users can't run the script, and the message
comes from the system ;-( ... it can't be a message from my script ;-(

#/usr/bin/python2.3
it will not be good, because my scrill will not work at all ;-)

#/usr/bin/python
it will select the defined python version of the platform ... And here
is the problem. On debian/sid, users have python2.3 (default), et py2.4
: So the script will start with 2.3, and my message will be displayed.
But it could work because there is a py2.4 on the machine ;-(.

I'd like to make my script (or a starter script)
which will be able to detect all python versions of the machine. And
run my script with the good one ; py2.4, or display a message to the
user to say her it must install py2.4 ...

I hope you understand my needs. Is there a python/bash mechanism to
override the default python version of the system ... and run the
script with any version of python (but the most recent) ?
or can you explain me how to do that ? the simplest way ?
 
L

Laszlo Zsolt Nagy

I've got a trouble, and i think that anybody there can help me

I've got a python script which i distribute in somes packages for *nix.
This script is full of python and need python 2.4 ! And i'd like to
display a message when the user doesn't have a python2.4 version.

>>> import sys
>>> sys.version '2.4.1 (#65, Mar 30 2005, 09:13:57) [MSC v.1310 32 bit (Intel)]'
>>>

There is also

sys.hexversion
sys.api_version
sys.version_info*

Read this:

*http://docs.python.org/lib/module-sys.html


Les

**
 
F

Fredrik Lundh

I've got a trouble, and i think that anybody there can help me

I've got a python script which i distribute in somes packages for *nix.
This script is full of python and need python 2.4 ! And i'd like to
display a message when the user doesn't have a python2.4 version.
I'd like to make my script (or a starter script)
which will be able to detect all python versions of the machine. And
run my script with the good one ; py2.4, or display a message to the
user to say her it must install py2.4 ...

the approach used in bzr might work for you:

#
# application boot script

import os, sys

try:
version_info = sys.version_info
except AttributeError:
version_info = 1, 5 # 1.5 or older

REINVOKE = "__MYAPP_REINVOKE"
NEED_VERS = (2, 4)
KNOWN_PYTHONS = ('python2.4',)

if version_info < NEED_VERS:
if not os.environ.has_key(REINVOKE):
# mutating os.environ doesn't work in old Pythons
os.putenv(REINVOKE, "1")
for python in KNOWN_PYTHONS:
try:
os.execvp(python, [python] + sys.argv)
except OSError:
pass
print >>sys.stderr, "error: cannot find a suitable python interpreter"
print >>sys.stderr, " (need %d.%d or later)" % NEED_VERS
sys.exit(1)

if hasattr(os, "unsetenv"):
os.unsetenv(REINVOKE)

#
# get things going!

import myapp
myapp.run()

# end of boot script

(the actual bzr source is shipped under the GPL, but 1) it's trivial, and 2) I contributed
the code in the first place, so I'd say it's safe to "steal this code" ;-)

</F>
 
R

Roel Schroeven

Laszlo said:
I've got a trouble, and i think that anybody there can help me

I've got a python script which i distribute in somes packages for *nix.
This script is full of python and need python 2.4 ! And i'd like to
display a message when the user doesn't have a python2.4 version.
'2.4.1 (#65, Mar 30 2005, 09:13:57) [MSC v.1310 32 bit (Intel)]'

Yes, but the problem is also that Debian (not only Sid, but also stable
and testing) has python 2.4 but it is not the default, i.e.
/usr/bin/python is a symlink to /usr/bin/python2.3 even if
/usr/bin/python2.4 is available to.

It would be nice if there was a general way to specify that a script
should be interpreted by python2.4, even if it is not the default, in a
way that works on all platforms. But I don't see how that should work.
 
L

Laszlo Zsolt Nagy

Roel said:
Laszlo Zsolt Nagy wrote:

(e-mail address removed) wrote:


I've got a trouble, and i think that anybody there can help me

I've got a python script which i distribute in somes packages for *nix.
This script is full of python and need python 2.4 ! And i'd like to
display a message when the user doesn't have a python2.4 version.




import sys
sys.version
'2.4.1 (#65, Mar 30 2005, 09:13:57) [MSC v.1310 32 bit (Intel)]'

Yes, but the problem is also that Debian (not only Sid, but also stable
and testing) has python 2.4 but it is not the default, i.e.
/usr/bin/python is a symlink to /usr/bin/python2.3 even if
/usr/bin/python2.4 is available to.
Hmm.

Idea one:

Create a list of possible locations. First of all, use your PATH, and
then add common locations:

['/bin','/usr/bin','/opt/bin','/usr/local/bin'] # etc.

Then use a list of the possible executable names:

['python','python2.3','python2.4'] # etc

Finally using these combinations, execute each executeable with

python -V

and examine its output. This may work, but I'm affraid there is no
general solution.
The system administrator can install different python versions to
virtually any location.
But I don't think you should be affraid of that. If a system admin
installs different
versions into strange locations, then he will take the responsibility to
fix python programs
that need a specific version. But in most cases, looking for a python
executable
on your PATH should be enough; and it is the most I would expect from an
application. :)

Last idea:

- create a configuration file that resides beside your Python program
- take the path to the good executeable there
- if the program was started with the wrong version, but you have
the path to the good one (from the config file), then re-execute
- otherwise print an error message telling the required version AND
how the user can set it up in the config file

Third idea (for Windows only): read available versions from the
registry. ;-)

Best,

Les
 
M

Michael Ekstrand

I hope you understand my needs. Is there a python/bash mechanism to
override the default python version of the system ... and run the
script with any version of python (but the most recent) ?
or can you explain me how to do that ? the simplest way ?

This solution makes a few assumptions... but it should work in the
majority of cases.

The principle is that Python is, in my experience, *usually* installed
as python2.4 or whatever - even ./configure && make && make install in
the tarball makes python a symbolic link to a python2.4 executable.
Assuming that this is the case, and that python2.4 will never be any
other version of Python:

#!/bin/sh

APPPATH=/path/to/app
PYTHON=`which python2.4`

if [ $? != 0 ]; then
echo "This program requires Python 2.4 to be installed." >/dev/stderr
exit 1
fi

"$PYTHON" "$APPATH" "$@"

Problems: Requires Python 2.4 to be installed as python2.4, and doesn't
have upward compatibility (i.e. 2.5). But it's at least as good as
#!/usr/bin/env python2.4, and it gives a clean error message.

-Michael
 
J

Jorgen Grahn

I've got a trouble, and i think that anybody there can help me

I've got a python script which i distribute in somes packages for *nix.
This script is full of python and need python 2.4 ! And i'd like to
display a message when the user doesn't have a python2.4 version.

if i put, at the top of the script (which can be done with /usr/bin env
too):

One data point, from the distutils manual
(http://docs.python.org/dist/node10.html):

Scripts are files containing Python source code, intended to be started
from the command line. Scripts don't require Distutils to do anything very
complicated. The only clever feature is that if the first line of the
script starts with #! and contains the word `python'', the Distutils will
adjust the first line to refer to the current interpreter location.

which I interpret as "distutils will force the script to run the same Python
as setup.py is running". Which, in turn, means that you could detect the
absence of Python 2.4 at install time.

/Jorgen
 

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,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top