« Python 3.0 and 2.6 and Executable Zipfiles »: a great way of distributing applications as a single file
Posted by patrick sur décembre 8, 2008
A new feature that was quietly sneaked into Python 2.6 (NDLR, and Python 3.0), without the fanfare it deserves, is the ability to distribute Python applications as executable zipfiles.
What is new is the ability to make zip archives executable. If you call the Python 2.6+ (or 3.0+) interpreter passing in a zip file instead of a Python file – the interpreter looks inside the zip file for a Python file named __main__.py (at the top-level) and executes it. The zip file can also contain all the (pure-Python only) modules and packages your app depends on.
This is a great way of distributing applications as a single file. The nice thing is that the Python interpreter doesn’t depend on the extension to recognise zipfiles, instead recognising them automagically.
Floris Bruynooghe notes in the comments that you can add a hash-bang line to a zipfile and make it executable:
$ cat > __main__.py print('hi there') ^D $ zip test.zip __main__.py adding: __main__.py (stored 0%) $ cat > hashbang.txt #!/usr/bin/env python3.0 ^D $ cat hashbang.txt test.zip > my_exec $ chmod +x my_exec $ ./my_exec hi there $