Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

@sydalmighty sydalmighty commented Feb 22, 2018

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

@MatiasKopp

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

@animena animena commented Jun 26, 2020

This saved my day, Thanks

@syddo

This comment has been minimized.

Copy link

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

This comment has been minimized.

Copy link

@Vresod Vresod commented Feb 3, 2021

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

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