Self-extracting Python Script

Revision as of 08:30, 7 December 2007 by Root (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Build a compressed self-extracting executable script on UNIX

This shows how to build a compressed self-extracting script. This is a handy way to package a group of python modules into a single compressed self-extracting executable script. This works on UNIX and Cygwin with Python 2.3 Final or later.

# This is a self-extracting executable.
# Execute this like any normal executable.
# You may need to "chmod a+x" this file.
# This is a binary ZIP file with a Python loader header.
# Bourne shell loader:
PYTHON=$(which python 2>/dev/null)
if [ ! -x "$PYTHON" ] ; then
    echo "Python not found!"
    exit 1
exec $PYTHON -c "
# Python loader:
import sys, os
if int(sys.version[0])<2:
    print 'Python version 2.3 final or greater is required.'
    print 'Your version is', sys.version
major = sys.version_info[0]
minor = sys.version_info[1]
releaselevel = sys.version_info[3]
if (major==2 and minor<3) or (major==2 and minor==3 and releaselevel!='final'):
    print 'Python version 2.3 final or greater is required.'
    print 'Your version is', sys.version
sys.path.insert(0, sys.argv[1])
del sys.argv[0:1]
import MY_SCRIPT
" $0 $@
# Zip file:

Assume the following situation:

* you have a script called with a main() function.
* your script imports a number of other modules in the local directory.
* you want to distribute this script to others as a single executable script that does not require installation or unpacking of all the other modules.

Step 1. Compress your project into a ZIP file:

   zip MY_SCRIPT.ZIP *.py

Step 2. Add the shell script header source to the ZIP file. Be sure to change the two references to MY_SCRIPT to the actual name of your script:


Step 3. Make the SCRIPT executable.

   chmod 775 SCRIPT

Step 4. Now run your self-extracting executable ./SCRIPT

This does not corrupt the ZIP file and it is a valid shell script even though it is binary. This is a wrapper, within a wrapper, within a wrapper, but it actually works and it isn't too complicated to build for most projects.

Python is able to run code inside of a ZIP file. The shell script header starts python which runs another pythin script header that loads and run the main function in your script.