Skip to main content

Build a minimal, bespoke CPython runtime from source with size reduction, static linking, and a zipped standard library.

Project description

buildpy-bundler

A lightweight, single-file Python build tool for compiling Python 3.11-3.14 from source with customizable configurations.

Features

  • Single-file script with no external dependencies (stdlib only)
  • Multiple build configurations (static, shared, framework)
  • Automatic dependency building (OpenSSL, bzip2, xz)
  • Size optimization options (stdlib zipping, selective module inclusion)
  • Cross-platform support (macOS, Linux, Windows)

Requirements

  • Python 3.10+ (to run the build script)
  • System tools: git, wget, tar, make
  • C compiler (gcc/clang)

Quick Start

# Build with default configuration (shared_mid)
buildpy

# Or use make
make

The built Python will be installed to ./build/install/python/.

Usage

usage: buildpy.py [-h] [-a CFG [CFG ...]] [-b OPTIMIZE_BYTECODE] [-c NAME] [-d]
                  [-n] [-e] [-i PKG [PKG ...]] [-m] [-o] [-p] [-r] [-v VERSION]
                  [-w] [-j JOBS] [-s] [-t TYPE] [-S] [-A] [--auto-reduce]
                  [--auto-config] [--auto-config-output PATH]
                  [--apply-reductions MANIFEST] [--reduction-copy DIR]
                  [--skip-ziplib] [--ziplib] [--install-dir DIR]

A python builder

options:
  -h, --help            show this help message and exit
  -a, --cfg-opts CFG [CFG ...]
                        add config options
  -b, --optimize-bytecode OPTIMIZE_BYTECODE
                        set optimization levels -1 .. 2 (default: -1)
  -c, --config NAME     build configuration (default: shared_mid)
  -d, --debug           build debug python
  -n, --dry-run         show build plan without building
  -e, --embeddable-pkg  install python embeddable package
  -i, --install PKG [PKG ...]
                        install python pkgs
  -m, --package         package build
  -o, --optimize        enable optimization during build
  -p, --precompile      precompile stdlib to bytecode
  -r, --reset           reset build
  -v, --version VERSION
                        python version (default: 3.13.11)
  -w, --write           write configuration
  -j, --jobs JOBS       # of build jobs (default: 4)
  -s, --json            serialize config to json file
  -t, --type TYPE       build based on build type
  -S, --size-report     show size breakdown of build
  -A, --analyze-deps    analyze stdlib dependencies of packages
  --auto-reduce         automatic workflow: analyze deps, build with
                        shared_vanilla, apply reductions, zip stdlib
  --auto-config         generate reduction manifest based on dependency analysis
  --auto-config-output PATH
                        output path for reduction manifest (default: reduction-
                        manifest.json)
  --apply-reductions MANIFEST
                        apply reduction manifest to remove unused files from
                        build
  --reduction-copy DIR  copy build to DIR before applying reductions (safer for
                        testing)
  --skip-ziplib         skip stdlib compression (use with --apply-reductions
                        workflow)
  --ziplib              compress stdlib of existing build (after --apply-
                        reductions)
  --install-dir DIR     custom installation directory (overrides --package)

Options

Option Description
-v, --version VERSION Python version to build (default: 3.13.11)
-c, --config NAME Build configuration (default: shared_mid)
-t, --type TYPE Build type: static, shared, or framework
-j, --jobs N Number of parallel build jobs (default: 4)
-o, --optimize Enable optimization during build
-d, --debug Build debug Python
-p, --precompile Precompile stdlib to bytecode
-b, --optimize-bytecode Set bytecode optimization level -1..2 (default: -1)
-m, --package Package the build for distribution
-r, --reset Clean and reset build directory
--install-dir DIR Custom installation directory (overrides --package)
-i, --install PKG [PKG ...] Install packages after build
-w, --write Write Setup.local configuration
-s, --json Export configuration to JSON
-a, --cfg-opts CFG [CFG ...] Add config options
-e, --embeddable-pkg Install python embeddable package
-n, --dry-run Show build plan without building
-S, --size-report Show size breakdown of build
-A, --analyze-deps Analyze stdlib dependencies of packages
--auto-reduce Automatic workflow: analyze, build, reduce, compress
--auto-config Generate reduction manifest based on analysis
--auto-config-output PATH Output path for reduction manifest
--apply-reductions MANIFEST Apply reduction manifest to remove unused files
--reduction-copy DIR Copy build to DIR before applying reductions
--skip-ziplib Skip stdlib compression (for reduction workflow)
--ziplib Compress stdlib of existing build

Examples

# Build Python 3.12 with static linking
buildpy -v 3.12 -t static

# Build optimized Python with 8 parallel jobs
buildpy -o -j 8

# Build and package for distribution
buildpy -m

# Build to custom directory
buildpy --install-dir /opt/python

# Build and install packages (e.g., for embedding)
buildpy -i requests numpy pandas

# Preview build plan without building (dry-run)
buildpy -n -c static_mid -v 3.12

# Analyze size of completed build
buildpy -S

# Analyze stdlib dependencies of packages
buildpy -A -i requests urllib3

# Generate reduction manifest based on dependency analysis
buildpy -A -i ipython --auto-config

Size-Optimized Builds

The --auto-reduce flag provides a single-command workflow for creating minimal Python distributions:

# Build Python with only the modules needed for numpy
buildpy -i numpy --auto-reduce

This command:

  1. Analyzes numpy's stdlib dependencies using AST-based import detection
  2. Builds (or reuses cached) vanilla Python with all modules as shared extensions
  3. Copies to python-shared-reduced and installs the specified packages
  4. Removes unused extension modules and stdlib directories
  5. Compresses the stdlib into a zip archive
  6. Verifies the build by importing each specified package

The vanilla build is cached at build/install/python-shared-vanilla for fast iterations.

Manual workflow (for more control):

# 1. Build without zipping stdlib
buildpy -c shared_vanilla --skip-ziplib

# 2. Apply reductions
buildpy --apply-reductions reduction-manifest.json

# 3. Compress the reduced stdlib
buildpy --ziplib

# Or apply to a copy for testing:
buildpy --apply-reductions reduction-manifest.json --reduction-copy build/reduced

Configurations

Configurations follow the naming pattern <build-type>_<size-type>:

Size Type Description
max Maximum modules included
mid Balanced selection (recommended)
min Minimal footprint
bootstrap Absolute minimum for bootstrapping
vanilla All modules as shared extensions (for --auto-reduce)

Static Builds

Statically links libpython into the executable.

Config Modules Excluded
static_max _ctypes only
static_mid _ssl, _hashlib
static_min Most optional modules
static_bootstrap Based on Setup.bootstrap

Shared Builds

Uses a shared libpython library.

Config Modules Excluded
shared_max None
shared_mid _ctypes, _ssl, _hashlib, _decimal
shared_min Most optional modules
shared_vanilla None (all as shared extensions for post-build reduction)

Framework Builds (macOS only)

Creates a macOS framework bundle. Not yet implemented.

Build Output

build/
  downloads/     # Cached source archives
  src/           # Extracted source trees
  install/
    python/      # Final Python installation

Output naming convention: py-<type>-<size>-<version>-<platform>-<arch>

Example: py-static-mid-3.13.0-darwin-arm64

Known Issues

  • If libb2 is installed on your system, the _blake2 module will link against it, creating a runtime dependency

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

buildpy_bundler-0.1.1.tar.gz (97.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

buildpy_bundler-0.1.1-py3-none-any.whl (36.5 kB view details)

Uploaded Python 3

File details

Details for the file buildpy_bundler-0.1.1.tar.gz.

File metadata

  • Download URL: buildpy_bundler-0.1.1.tar.gz
  • Upload date:
  • Size: 97.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.2

File hashes

Hashes for buildpy_bundler-0.1.1.tar.gz
Algorithm Hash digest
SHA256 a6728c1c8dacb7f25eebf19c7c7a76fe6eea0613236bafc1526f7b4ce5a31382
MD5 0ff5b6d55f8ea74e68deb5cf9b3c4dc9
BLAKE2b-256 7a61027405536c34713fb812d5d6d3873b806bdb70870065867c2b268091f25b

See more details on using hashes here.

File details

Details for the file buildpy_bundler-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for buildpy_bundler-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 103bd78df57f141e2133f2c93ffdbffb1bae18d20ecd1bd7a5191109541b83ab
MD5 a0df6ce1d8fe180546d2546acf961bd2
BLAKE2b-256 d39a9e3fe065389ae8313177cb487e67a8e689b389f662a3054306cb4abd794e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page