When writing CLI applications I often find myself deciding on whether to use Go or Python - the former creating single-file (binary) executables (handy for distribution), and the latter being easy to develop. However, I recently discovered zipapp; A part of the Python standard library that takes your Python files and shoves them into a single (zipped) file that you can execute - giving one of the benefits Go binaries have (with them otherwise being quite significantly different).
Zipapp is a Python stdlib module that takes your files in a package and zips ‘em to a single file Python can interpret. Here’s an example of a useless application packaged with zipapp:
1 |
|
1 |
|
With this you should be able to run:
1 |
|
Now to package the greet
directory into a single binary:
1 |
|
Ok let’s complicate this by using an external pip module. Zipapp won’t automatically include referenced pip modules but there’s a way to get around it; by installing modules directly to you app directory; Let’s make our app even more useless with cowsay
:
1 |
|
1 |
|
Now to package our app:
1 |
|
Beautiful!
I would suggest you create the build from another directory than your application code to not get hell when creating your .gitignore
. A simple build script could look something like this
1 |
|
1 |
|
For a full example visit github.com/palmenhq/python-zipapp-example
This is a really nifty way of building Python cli tools imo. However, note that it’ll use the host’s Python interpreter, so you can’t run it if Python is not installed. However, most systems have python3 installed so this should be more of an advantage than a problem, as that can keep bundle file size down.
When building cli applications, my go-to option will from now on be Python unless I want the raw speed Go offers.