build-your-python
A Python library to build the CPython interpreter, in a modular, extensible, and freestanding way. This project is an alternative build pipeline for CPython, based on the Zig build system. It can be run in two ways:
- directly from source (this repo) with a Zig compiler (0.17.x), the only mandatory dependency of this package.
- from Python by installing the Python bindings (
pip install build-your-python). No system dependency needed, it is standalone.
The manifest below should give deeper insights on the raison d'être of this project.
The goals are similar to Astral's standalone Python.
This project however takes the process one step further: it exposes all of CPython's compilation flags and optional behavior as part of a user-controlled interface, and adds cross-compilation support (whereas the Astral project is more CI/CD oriented, its main goal being to produce one single artifact).
The typical use case is integration into tooling that builds standalone binaries out of Python projects, typically to build a custom-tailored interpreter for that project. It provides mostly four things that you do not get from the CPython source itself:
- lightweight build: you only need the Zig compiler (if you install this from PyPI, it is installed automatically for you), which is itself a standalone binary. Nothing else is required.
- standalone build pipelines not only for the interpreter itself but also for most of the 3rd party dependencies it relies on (the exceptions are ncurses, readline and Tcl/Tk, which only support dynamic linking; see Limitations), so you don't have to dynamically link them, making your interpreter executable truly standalone.
- selecting what goes into your interpreter (i.e., you can include only the subset of the 3rd party libraries mentioned above that you need), and the related options, from one unified top-level interface, controllable directly from Python.
- native cross-compilation support, including for the vendored 3rd party dependencies (see Limitations for tested targets).
Manifest
The CPython executable distributed on most systems is not a standalone piece of software.
If you copy that binary to another system, even with the same operating system and CPU architecture,
it is not guaranteed to work there.
The reason is that the default interpreter binary is usually dynamically linked against a bunch of system libraries,
which become hidden dependencies of your interpreter and, by extension, of your Python projects.
CPython is shipped on many Linux distributions as it's heavily used for housekeeping/scripting, and since these distributions also happen to be responsible for shipping said dynamic libraries,
that approach works pretty well for them.
However, once you use Python as a programming language rather than a scripting one, the fact that the
interpreter itself is not a freestanding piece of software gets mildly annoying; once you have to distribute
binaries at large scale, maybe on a wide range of platforms, it becomes extremely annoying, if not a real deal-breaker.
Distributing binaries reliably is no longer trivial.
The good news is that dynamically linking these 3rd party dependencies is not a necessity by design. It is mostly a consequence of CPython being shipped by Linux distributions (which can afford to manage them directly since they are also responsible for them), and of historical practices in the C/C++ build ecosystem.
Thus, this project provides the following:
- A
build.zig-based pipeline to build the CPython interpreter, which by itself already has two immediate benefits:- Building only requires the Zig compiler (0.17.x) and nothing else, which is itself downloadable as a freestanding binary.
- You can cross-compile the interpreter, as the Zig compiler supports cross-compilation natively.
- Similar
build.zigpipelines for most 3rd party libraries that CPython depends on (see vendors in this package). Some of them already had an existing Zig pipeline (see allyourcodebase), in which case this project uses them directly; for the others we vendor our own version. They support cross-compiling, which is one of the main requirements. - A programmatic interface on top of all the compile-time options/flags that CPython supports.
- Python bindings on top of all of that, abstracting the Zig compiler away and providing a similar programmatic interface on the Python side.
Building with zig build
zig build
Produces zig-out/bin/python (+ zig-out/lib/pythonX.Y) for the host
target, CPython 3.12.13 (default), with the default library linkages.
Selecting a CPython version
zig build -Dcpython-version=3.13.8
Curated versions: 3.11.13, 3.12.13 (default), 3.13.8, 3.14.2,
3.15.0 (see build.zig.zon). Any other source (a fork, an
unreleased tag) can be added with zig fetch --save=<name> <url-or-git-ref>
run from this project root, then selected the same way:
-Dcpython-version=<name>.
To build from an already-extracted tree instead (e.g. the checked-in
cpython/ submodule), use -Dcpython-path=<dir> — this bypasses
-Dcpython-version entirely.
Debug builds vs. Zig's own optimization levels
-Doptimize=... is Zig's build-mode flag, but it does double duty here:
it also selects CPython's own configure-time build mode, not just how the
glue/build-system code around it is compiled.
-Doptimize=Debug→ configures CPython with--with-pydebug: a real Python debug build (Py_DEBUG, extra runtime assertions, reference count debugging, adABI suffix on extension modules). It does not only set Zig's own compile mode: it changes CPython's ABI and runtime behavior.-Doptimize=ReleaseFast/-Doptimize=ReleaseSafe/-Doptimize=ReleaseSmall→ CPython is configured with--enable-optimizationsand without--with-pydebug. CPython's ownOPTvariable (-DNDEBUG -O3, set by./configurefor any non-debug build, independently of that flag) — not Zig's own per-mode default (-O2,-O2,-Osrespectively) — is what CPython's C sources actually get compiled with:./configure's flags are passed explicitly per file and land after Zig's own optimize-derived flags in the compile command, and the last-Oflag on the line wins. So all three modes produce CPython object code built at the same optimization level; they only differ in Zig-internal defaults (safety-check codegen) that have no effect on CPython's own sources here. Note that--enable-optimizationsupstream additionally means PGO, but that step belongs to CPython'smakerule, which this pipeline does not run: no PGO is performed.
Independently of -Doptimize, -Dstrip=true|false controls whether debug
info/symbols are stripped from the produced artifacts at link time (default:
Zig's own per-optimize-mode default). This is unrelated to --with-pydebug
— it only affects whether DWARF/symbols ship in the binaries, not CPython's
build mode or ABI.
# Debug (Py_DEBUG) build of 3.13.8
zig build -Doptimize=Debug -Dcpython-version=3.13.8
# Optimized (--enable-optimizations) release build, stripped
zig build -Doptimize=ReleaseFast -Dstrip=true
Per-library linkage
Every 3rd party dependency has its own -D<lib>-linkage= option.
Libraries are independent: you can static-link OpenSSL while keeping SQLite dynamic.
| Library | Accepted values | Default |
|---|---|---|
openssl, zlib, libffi, sqlite, bz2, lzma |
static, dynamic, off |
static |
ncurses, readline, tk |
dynamic, off only (no vendored build; static is rejected) |
dynamic |
static: vendored sources are built and linked into the interpreter (default for the libraries that have a vendored build).dynamic: CPython's stock behavior, linking against the library found on the build/target system.off: the library, and the extension modules depending on it, are left out.
Two global options are orthogonal to these:
-Dlibc-linkage=static|dynamic: fully static (musl) or dynamically linked libc.-Dpython-linkage=static|dynamic|off: buildlibpythonX.Yas a library, or compile everything into one monolithic executable (off, default).
# static everything (default), except SQLite taken from the system, and no lzma
zig build -Dsqlite-linkage=dynamic -Dlzma-linkage=off
Limitations
Standalone-ness and cross-compilation are not uniform across the whole interpreter.
- Windows: not supported yet (
zig buildpanics for Windows targets). CPython's Windows build is driven by MSBuild project files rather than./configure, so the POSIX pipeline does not carry over. tkinter/turtle/ IDLE: no static support, and none planned. Tcl/Tk is not vendored;-Dtk-linkagesupports onlydynamic(system library) oroff.curses(_curses,_curses_panel): ncurses is not vendored;-Dncurses-linkagesupports onlydynamic(system library) oroff.readline: not vendored (no suitable package);-Dreadline-linkagesupports onlydynamic(system library) oroff. The interpreter works without it, but the REPL then has no line editing/history._uuid,_gdbm,_dbm: not built.uuid.uuid1()/uuid4()anddbm.dumbwork as pure-Python fallbacks.nis: not supported (removed from CPython 3.13).- Fully static builds (
-Dlibc-linkage=static): musl-static has no workingdlopen. Extension modules must be built into the interpreter, third-party C-extension wheels (manylinux/glibc) cannot be loaded, andctypes.CDLLof arbitrary.sofiles does not work. Use-Dlibc-linkage=dynamic, or-Dpython-linkage=dynamic, to regaindlopen. - OpenSSL: built with assembly optimizations disabled, so cryptographic performance is lower than stock builds.
- Cross-compilation: 3rd party libraries are verified on
x86_64-linux-muslandaarch64-linux-musl(the latter under QEMU for OpenSSL). Other targets (including macOS) are not tested. Cross-compiled interpreters cannot be run on the build host, so they are not smoke-tested at build time. - Relocatability: CPython bakes absolute paths (
sys.prefix,sysconfig, RPATH) at build time; relocating the output tree may require extra patching.
Metadata
Release files for build-your-python 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| build_your_python-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for build_your_python-0.1.0-py3-none-any.whl |
Release files / build_your_python-0.1.0-py3-none-any.whl
| Download URL | build_your_python-0.1.0-py3-none-any.whl |
|---|---|
| Size | 47.4 MB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ef57e48d2c4e53285ef6b458d589d797671234ada2899eb859fe0d77d808a60d
|
|
BLAKE2b-256 checksum How to use checksums |
2d8a826fb5965b42940b87d7544c5570224c3024c4c3ec32482097629fc06462
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
uv/0.13.9 {"installer":{"name":"uv","version":"0.13.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Security for build-your-python 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.