Static binaries in Python

Static binaries in Python

Do you envy developers that use Go, Rust or Haskell for their static binaries? Just one file that can be passed around, like the binary of the static[1] website generator Hugo, written in Go?

No need to install libraries (of course you need to install a certain version that conflicts with what is already installed on your system) and so on. Just a program that can be used "as is".

Python has Virtualenv, but it’s not quite the same as a static binary. To run a Python program with Virtualenv, you have to set up the environment, then activate it. This is all fine and dandy (and superuseful) for developers, but as a user of a program, you don’t want all that hassle.

You can have that kind of convenience with Python, too. Just use Pyinstaller[2] with the appropriate option, like so:

pyinstaller poet.py --onefile

Your binary can then be found under the path dist/poet in the current directory.

"What about size?", you may ask. After all, the binary has a Python interpreter baked in. Well, the resulting binary is bigger than a similar binary created with Haskell, but it’s not monstrous. The Python binary for the little program (the source code is less than one hundred lines) used in the example here is 5.8 Megabyte. The same program written in Haskell[3] has a size of 1.6 Megabyte. Judge for yourself.


1. I’m sure you already know, but just in case: Static means something different in the context of websites. It means something like "no moving parts" there. Here, it means something like "no linked libraries, all baked together into one blob".

links

social