Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #75053 > unrolled thread

Distributing python applications as a zip file

Started bySteven D'Aprano <steve@pearwood.info>
First post2014-07-23 04:23 +0000
Last post2014-07-24 00:55 +0000
Articles 10 — 10 participants

Back to article view | Back to comp.lang.python


Contents

  Distributing python applications as a zip file Steven D'Aprano <steve@pearwood.info> - 2014-07-23 04:23 +0000
    Re: Distributing python applications as a zip file Gary Herron <gary.herron@islandtraining.com> - 2014-07-22 22:30 -0700
    Re: Distributing python applications as a zip file Chris Rebert <clp2@rebertia.com> - 2014-07-22 22:46 -0700
    Re: Distributing python applications as a zip file Tim Golden <mail@timgolden.me.uk> - 2014-07-23 08:20 +0100
    Re: Distributing python applications as a zip file Chris Angelico <rosuav@gmail.com> - 2014-07-23 17:59 +1000
    Re: Distributing python applications as a zip file Thomas Heller <theller@ctypes.org> - 2014-07-23 10:07 +0200
    Re: Distributing python applications as a zip file Leo Jay <python.leojay@gmail.com> - 2014-07-23 16:43 +0800
      Re: Distributing python applications as a zip file Alan <alan.isaac@gmail.com> - 2014-07-24 05:19 -0700
    Re: Distributing python applications as a zip file Burak Arslan <burak.arslan@arskom.com.tr> - 2014-07-23 15:23 +0300
      Re: Distributing python applications as a zip file Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-07-24 00:55 +0000

#75053 — Distributing python applications as a zip file

FromSteven D'Aprano <steve@pearwood.info>
Date2014-07-23 04:23 +0000
SubjectDistributing python applications as a zip file
Message-ID<53cf38c2$0$29897$c3e8da3$5496439d@news.astraweb.com>
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@runes:~$ cat __main__.py 
print("NOBODY expects the Spanish Inquisition!!!")

steve@runes:~$ zip appl __main__.py 
  adding: __main__.py (stored 0%)
steve@runes:~$ rm __main__.py 
steve@runes:~$ python appl.zip 
NOBODY expects the Spanish Inquisition!!!


On Linux, you can even hack the zip file to include a shebang line!


steve@runes:~$ cat appl
#!/usr/bin/env python
# This is a Python application stored in a ZIP archive.
steve@runes:~$ cat appl.zip >> appl
steve@runes:~$ chmod u+x appl
steve@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.



-- 
Steven

[toc] | [next] | [standalone]


#75057

FromGary Herron <gary.herron@islandtraining.com>
Date2014-07-22 22:30 -0700
Message-ID<mailman.12216.1406093800.18130.python-list@python.org>
In reply to#75053
On 07/22/2014 09:23 PM, Steven D'Aprano 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@runes:~$ cat __main__.py
> print("NOBODY expects the Spanish Inquisition!!!")
>
> steve@runes:~$ zip appl __main__.py
>    adding: __main__.py (stored 0%)
> steve@runes:~$ rm __main__.py
> steve@runes:~$ python appl.zip
> NOBODY expects the Spanish Inquisition!!!
>
>
> On Linux, you can even hack the zip file to include a shebang line!
>
>
> steve@runes:~$ cat appl
> #!/usr/bin/env python
> # This is a Python application stored in a ZIP archive.
> steve@runes:~$ cat appl.zip >> appl
> steve@runes:~$ chmod u+x appl
> steve@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.
>
>
>

Really!  20 years of Pythoning, and I'd never seen this!  When was this 
introduced?

Gary Herron

[toc] | [prev] | [next] | [standalone]


#75058

FromChris Rebert <clp2@rebertia.com>
Date2014-07-22 22:46 -0700
Message-ID<mailman.12217.1406094386.18130.python-list@python.org>
In reply to#75053
On Tue, Jul 22, 2014 at 9:23 PM, Steven D'Aprano <steve@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.
<snip>
> 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.

And if you want something nearly completely self-contained (probably
modulo dynamic linking), it seems that there's PEX
(http://pex.readthedocs.org/en/latest/ ).

Cheers,
Chris

[toc] | [prev] | [next] | [standalone]


#75064

FromTim Golden <mail@timgolden.me.uk>
Date2014-07-23 08:20 +0100
Message-ID<mailman.12221.1406100053.18130.python-list@python.org>
In reply to#75053
On 23/07/2014 06:30, Gary Herron wrote:
> On 07/22/2014 09:23 PM, Steven D'Aprano wrote:
>> A little known feature of Python: you can wrap your Python application in
>> a zip file and distribute it as a single file.


 > Really!  20 years of Pythoning, and I'd never seen this!  When was this
> introduced?

This post by Brett Cannon is useful:


http://sayspy.blogspot.co.uk/2010/03/various-ways-of-distributing-python.html

I was trying to track down a presentation in the same vein which I saw
him give at EuroPython a few years ago, but I can't seem to find it. It
basically says the same thing but it's a slightly clearer read.

TJG

[toc] | [prev] | [next] | [standalone]


#75068

FromChris Angelico <rosuav@gmail.com>
Date2014-07-23 17:59 +1000
Message-ID<mailman.12224.1406102395.18130.python-list@python.org>
In reply to#75053
On Wed, Jul 23, 2014 at 2:23 PM, Steven D'Aprano <steve@pearwood.info> wrote:
> On Linux, you can even hack the zip file to include a shebang line!
>
>
> steve@runes:~$ cat appl
> #!/usr/bin/env python
> # This is a Python application stored in a ZIP archive.
> steve@runes:~$ cat appl.zip >> appl
> steve@runes:~$ chmod u+x appl
> steve@runes:~$ ./appl
> NOBODY expects the Spanish Inquisition!!!

This, by the way, depends on a feature of the zip file format: you
start reading from the back, with the key indexes, and then come to
the front. It's designed to allow various self-extracting archive
formats to be easily unzipped (imagine, if you will, a SFX built for
Windows when you're on Unix - rather than try to run the program (with
all the difficulties and risks that would entail), you just unzip it),
and it works nicely here too. I suppose, then, it would be possible to
make a minimal Unix SFX prefix: "#!/usr/bin/env unzip\n" on the
beginning of a zip should do the job :)

(Yes, I'm aware that that violates most of the point of an SFX, in
that the target system doesn't need to have pkunzip installed, but
it's still neat how short it can be.)

ChrisA

[toc] | [prev] | [next] | [standalone]


#75070

FromThomas Heller <theller@ctypes.org>
Date2014-07-23 10:07 +0200
Message-ID<c398qqFigoU1@mid.individual.net>
In reply to#75053
Am 23.07.2014 06:23, schrieb Steven D'Aprano:
> 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.

Look here:

http://legacy.python.org/dev/peps/pep-0441/
https://pypi.python.org/pypi/pyzzer
https://pypi.python.org/pypi/pyzaa/0.1.0

Or write your own little utility to create such a thing, it's not 
complicated.

Thomas

[toc] | [prev] | [next] | [standalone]


#75071

FromLeo Jay <python.leojay@gmail.com>
Date2014-07-23 16:43 +0800
Message-ID<mailman.12226.1406104999.18130.python-list@python.org>
In reply to#75053
On Wed, Jul 23, 2014 at 12:23 PM, Steven D'Aprano <steve@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@runes:~$ cat __main__.py
> print("NOBODY expects the Spanish Inquisition!!!")
>
> steve@runes:~$ zip appl __main__.py
>   adding: __main__.py (stored 0%)
> steve@runes:~$ rm __main__.py
> steve@runes:~$ python appl.zip
> NOBODY expects the Spanish Inquisition!!!
>
>
> On Linux, you can even hack the zip file to include a shebang line!
>
>
> steve@runes:~$ cat appl
> #!/usr/bin/env python
> # This is a Python application stored in a ZIP archive.
> steve@runes:~$ cat appl.zip >> appl
> steve@runes:~$ chmod u+x appl
> steve@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

[toc] | [prev] | [next] | [standalone]


#75139

FromAlan <alan.isaac@gmail.com>
Date2014-07-24 05:19 -0700
Message-ID<623b775e-4d9b-4274-add5-1036bc7d26c1@googlegroups.com>
In reply to#75071
On Wednesday, July 23, 2014 4:43:11 AM UTC-4, Leo jay wrote:
> 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


It looks like this was fixed for 3.2.  Was the fix ever backported to 2.7?

-- 
Thanks,
Alan Isaac

[toc] | [prev] | [next] | [standalone]


#75081

FromBurak Arslan <burak.arslan@arskom.com.tr>
Date2014-07-23 15:23 +0300
Message-ID<mailman.12236.1406118723.18130.python-list@python.org>
In reply to#75053
On 07/23/14 07:23, Steven D'Aprano 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@runes:~$ cat __main__.py 
> print("NOBODY expects the Spanish Inquisition!!!")
>
> steve@runes:~$ zip appl __main__.py 
>   adding: __main__.py (stored 0%)
> steve@runes:~$ rm __main__.py 
> steve@runes:~$ python appl.zip 
> NOBODY expects the Spanish Inquisition!!!
>
>

does it support package_data? or more specifically, does
pkg_resources.resource_* detect that the script is running from a zip
file and adjust accordingly?

[toc] | [prev] | [next] | [standalone]


#75108

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2014-07-24 00:55 +0000
Message-ID<53d0597d$0$29966$c3e8da3$5496439d@news.astraweb.com>
In reply to#75081
On Wed, 23 Jul 2014 15:23:10 +0300, Burak Arslan wrote:

> On 07/23/14 07:23, Steven D'Aprano wrote:
>> A little known feature of Python: you can wrap your Python application
>> in a zip file and distribute it as a single file.
[...]
> does it support package_data? or more specifically, does
> pkg_resources.resource_* detect that the script is running from a zip
> file and adjust accordingly?

No idea, sorry. Why don't you try it and see? Please let us know what you 
find.

-- 
Steven

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web