Some python applications contain a main skript and some additional modules. By using zipimport from python-2.3 and some help of the shell one may bind all needed files (except the python interpreter and its standard modules) into one executable. This eliminates the need for an expensive installation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#!/bin/sh PYTHON23=$(which python2.3 2>/dev/null) if [ ! -x "$PYTHON23" ] ; then echo "Python-2.3 executable not found - can't continue!" exit 1 fi exec $PYTHON23 - $0 $@ << END_OF_PYTHON_CODE import sys sys.path.insert(0, sys.argv) del sys.argv[0:2] import main main.main() END_OF_PYTHON_CODE
This solution was tested with Linux, but will work with other UNIX-like systems. It needs Python-2.3 final (2.3beta will not work!).
Let's assume you have a python application with some modules (spam.py and eggs.py). The main function is called main() in the file main.py.
- First you need to zip all files: $ zip main.zip spam.py eggs.py main.py (you may use the zipfile module instead)
- Save the code above to the file zipheader.unix
- Then concatenate both files: $ cat zipheader.unix main.zip > standalone
- Last step is to make standalone executable: $ chmod +x standalone
Done! All of your application code is contained in the file "standalone". When started, the shell replaces itself by the Python interpreter. The interpreter will reopen the file as a zipfile (skipping the 'leading garbage') and find all needed modules inside the zipfile.
I tried to implement the same for a batchfile on windows, but without success so far. ;-(