Reputation: 402
Given a set of python programs:
/tool/a.py
/tool/b.py
/tool/c.py
/tool/d.py
...
that are stored in a shared network directory, executed in a mixed environment (Solaris and different flavors of Linux) and that all requires a specific python version that may not be in the users $PATH and may not be installed in the same location on the different types of machines.
How can the programs specify what python interpreter to use?
Alternatives I've considered:
A shebang in each python program, refereeing to a python wrapper that starts an appropriate python interpreter for the current type of machine. But execve does not allow the wrapper executable to be implemented as a shell script and compiling native executables for each machine would require a lot of maintenance.
Making a startup shell script for each python program. All the shell scripts may share the same logic for choosing a python interpreter, but I would like to avoid having a separate shell script for each python program, if possible.
Making some kind of hack to make each program possible to run both both as a shell script and a python program, similar to:
"""exec" /tool/python_wrapper "$0" "$@" """#" def foo(): print "foo" foo()
Do you have some other ideas?
Upvotes: 1
Views: 2130
Reputation: 83348
You can directly forcing Python interpreter by giving .py file parameter to the Python binary:
python2.6 /tool/a.py
python2.4 /tool/b.py
To maintain different Python environments and different dependencies (Python eggs or native extensions) for each tool you want to use virtualenv:
http://pypi.python.org/pypi/virtualenv
Upvotes: 1
Reputation: 361605
I would do option #1 and use env
to get around the interpreter restriction. Note that Python may have already created version-specific binaries in /usr/bin
(it did on my system):
#!/usr/bin/env python2.4
Or
#!/usr/bin/env python2.6
Upvotes: 1
Reputation: 23514
I know this isn't quite the answer you are looking for, but I'd just make sure the python executables are in the PATH
and then use env
to find them. In the long run, I think this will be a lot less maintenance and headache. E.g.
#!/usr/bin/env python2.5
print "Hello from python 2.5!"
#!/usr/bin/env python2.6
print "Hello from python 2.6!"
#!/usr/bin/env python2.7
print "Hello from python 2.7!"
Upvotes: 4
Reputation: 9481
I would go with option 3. It will have a small startup delay, but it is the most flexible option.
One would not work, since you are running in mixed environments it will be a nightmare to set everything up correctly.
Would work but, as you said you'll need to maintain both the .py program and the shell script. Also it is not much different then 3.
Upvotes: 1