/usr/bin/env python, force a version

Fredrik Lundh fredrik at pythonware.com
Thu Oct 6 07:46:45 EDT 2005


"manatlan at gmail.com" 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.

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






More information about the Python-list mailing list