Distributing python applications as a zip file

Leo Jay python.leojay at gmail.com
Wed Jul 23 10:43:11 CEST 2014


On Wed, Jul 23, 2014 at 12:23 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> A little known feature of Python: you can wrap your Python application in
> a zip file and distribute it as a single file. The trick to make it
> runnable is to put your main function inside a file called __main__.py
> inside the zip file. Here's a basic example:
>
> steve at runes:~$ cat __main__.py
> print("NOBODY expects the Spanish Inquisition!!!")
>
> steve at runes:~$ zip appl __main__.py
>   adding: __main__.py (stored 0%)
> steve at runes:~$ rm __main__.py
> steve at runes:~$ python appl.zip
> NOBODY expects the Spanish Inquisition!!!
>
>
> On Linux, you can even hack the zip file to include a shebang line!
>
>
> steve at runes:~$ cat appl
> #!/usr/bin/env python
> # This is a Python application stored in a ZIP archive.
> steve at runes:~$ cat appl.zip >> appl
> steve at runes:~$ chmod u+x appl
> steve at runes:~$ ./appl
> NOBODY expects the Spanish Inquisition!!!
>
>
> It's not quite self-contained, as you still need to have Python
> installed, but otherwise it's a good way to distribute a Python
> application as a single file that users can just copy and run.
>

But if you use windows and you happen to use multiprocessing,
please be aware of this bug I encountered several years ago.
https://mail.python.org/pipermail/python-dev/2011-December/115071.html

-- 
Best Regards,
Leo Jay



More information about the Python-list mailing list