Truly portable executables written in python in 2023

160 views Asked by At

I've recently spent a lot of time figuring out how to distribute tools written in Python as standalone, self-contained executables, that will works on most of the linux distribution without special environment (including python itself). Decided to write this post to discuss my approach.

Here are a few simple steps I've come up with to make this trick:

  1. This first is very obvious - use special tooling that can make such an executable, like Nuitka, Pyoxidizer, PyInstaller, python-appimage, etc.

  2. Build your application against old enough glibc. You can do this using some old Linux distribution or just be setting up such toolchain manually. Its matters for c extensions, tools like nuitka/pyoxidizer and the python itself.

  3. Last but not least - don't forget about dependencies and the dependencies of your dependencies. All Python wheels should have either an any platform tag or one of the manylinux tags that suits your requirements. You can check tags and repair the bad ones with auditwheel. But fortunately, in the last 5-10 years, most of the mainstream packages have received manylinux wheels.

Of course, there will be issues with PyOxidizer/Nuitka/pyinstaller/etc pitfalls, especially with big projects, so you need some e2e tests in every Linux you want to support.

For educational purposes, I made this little repo. It includes:

  1. A sample app with bad dependencies and a C extension.
  2. A script to create a 'wheelhouse' with compatible wheels only (manylinux* and any).
  3. Scripts to build executables using Nuitka, PyOxidizer, Appimage, pex.
  4. Portability tests (run executables in different linux distros) and performance benchmarks.
  5. Readme and Makefile to run any step with single command.

It's obviously not a very useful repo, but at least it helped me to explore this topic a little, maybe it could help to demonstrate my approach to the topic. I'd be grateful for any feedback and thoughts on that topic! Please, prove me wrong, it would be very helpful!

0

There are 0 answers