Statically built python binary
Introduce a statically linked python interpreter


browse  log 



You can also use your local clone with git send-email.

#Statically built python binary

This is a set of scripts that allows you to build a python interpreter that is statically linked and can be run on pretty much any modern Linux distribution without installing any OS libraries (even in a completely bare Docker container).

This is possible because all dependencies are linked into the python binary itself.

You may need it if you write a Python script or a small program that you want to run for system administration or other purposes on wide range of environments, but don't want to build it for every OS imaginable. The static Python introduced in this repo can be bundled together with your script.

#How to build

You'd need Nix for building Python in this repo. I wish it was easier, but unfortunately only Nix provides a simple-to-use and completely static set of compilers and system libraries. Fortunately, you'd need Nix only for building, and not for running. And the resulting package doesn't depend on it in any way.

To install Nix, you can use an installer from here which is just one command on Linux or Mac.

After that, do the following in the root of this repository:

nix build

After the build completes, you'll have a result/ directory which will contain your statically linked Python.

#How it works

In theory, python codebase already has most of the required support for building the interpreter statically, and this can be done for WebAssembly to run the interpreter in the browser. However, it is hardcoded in a few places and thus it doesn't work when you try to build a static version for normal OS.

To make it work, all built-in modules that are normally shared libraries should be turned to static ones. For some, you can toggle this with a configuration flag, but there are a few that will still be linked dynamically and the build will fail.

This repo introduces a simple patch that adds an additional toggle to the configuration script.

#Doing this manually

Alright, so if you insist doing things without Nix, and you have a static Musl libc and a static toolchain, you can do the same yourself.

First, apply staticbuild.patch in the root of Python 3.12 repo like this:

patch -p1 < ../path/to/staticbuild.patch

Then run ./configure like this:

MODULE_BUILDTYPE=static ./configure --disable-test-modules --disable-xxlimited-modules

Then run make and you'd have your static python.