Skip to content

Instantly share code, notes, and snippets.

@asimjalis
Created December 7, 2012 23:33
Show Gist options
  • Star 25 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save asimjalis/4237534 to your computer and use it in GitHub Desktop.
Save asimjalis/4237534 to your computer and use it in GitHub Desktop.
How to deploy a Python application as a zip file

How to deploy a Python application as a zip file

by Asim Jalis, MetaProse.com

Create a file __main__.py containing:

print "Hello world from Python"

Zip up the Python files (in this case just this one file) into app.zip by typing:

zip app.zip *

The next step adds a shebang to the zip file and saves it as app—at this point the file app is a zip file containing all your Python sources.

echo '#!/usr/bin/env python' | cat - app.zip > app
chmod 755 app

That’s it. The file app is now have a zipped Python application that is ready to deploy as a single file.

You can run app either using a Python interpreter as:

python app

Or you can run it directly from the command line:

./app
@afeblot
Copy link

afeblot commented Oct 14, 2017

Doing it with Python:

#!/usr/bin/env python

import os
import shutil

appDir = 'appDir'
appName = 'app'
zipName = '%s.zip'%appName
shutil.make_archive(appName, 'zip', appDir)

with open(appName, 'wb') as appf:
    appf.write('#!/usr/bin/env python\n')
    with open(zipName, 'rb') as zipf:
        shutil.copyfileobj(zipf, appf)

os.chmod(appName, 0o755)
os.unlink(zipName)

@sydalmighty
Copy link

I always get:
/usr/bin/env: Event not found

@MatiasKopp
Copy link

MatiasKopp commented Sep 11, 2018

Supposing that your project has the current structure:
/project
|_ __main__.py
|_ __init__.py
|_ ...

You could just use zipapp mod from python and just execute:
python -m zipapp project -p "/usr/bin/env python"

@animena
Copy link

animena commented Jun 26, 2020

This saved my day, Thanks

@syddo
Copy link

syddo commented Sep 22, 2020

is there a way say to include the python interpreter itself in the zip file to execute it as an app?

@Vresod
Copy link

Vresod commented Feb 3, 2021

is there a way to use images and other resources located within the zip

@rowlesmr
Copy link

rowlesmr commented May 1, 2021

is there a way say to include the python interpreter itself in the zip file to execute it as an app?

have a look at pyinstaller

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment