Skip to main content

Compile Python applications into standalone native binaries

Project description

PyBend ๐Ÿชถ

Compile Python applications into standalone native binaries that run entirely in volatile memory.

PyBend is a zero-configuration, high-performance Python-to-Native-Binary compiler toolchain. It compiles your Python applications, third-party packages, and native C-extensions into a single, standalone executable โ€” with zero physical disk I/O at runtime.

Unlike traditional freezers (PyInstaller, cx_Freeze, Nuitka) that extract dependencies to /tmp or %TEMP%, PyBend keeps everything in RAM via a custom PEP 451 meta-path importer backed by an embedded Virtual File System.


๐Ÿš€ Performance Snapshot

Metric PyBend PyInstaller
Cold-Start Latency ~143 ms ~300โ€“500 ms
Disk I/O at Runtime 0 physical writes Writes to /tmp
Binary Size (hello world) ~2.7 MB ~30 MB+
Tree-Shaken Stdlib Modulefinder trace (300-400 modules) Bundles full stdlib (~3,400 modules)
Code on Disk Never Extracted to temp

๐Ÿ› ๏ธ How It Works

1. AST Tracing & Tree-Shaking

Scans your import graph using Python's modulefinder. Only the modules your app actually imports are included โ€” no bloat.

2. Optimization Pass (-OO)

All source is compiled to bytecode with assertions and docstrings stripped, yielding 8โ€“15% smaller payloads.

3. Dual-Table VFS v2 Layout

Bytecode, native .so/.pyd extensions, and static assets are packed into a structured binary blob. All entry payloads are compressed as a single Zstd block for maximum ratio:

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  BENDVFS BLOB                                     โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚  [4B: "BEND" Magic] [4B: Code Entry Count]       โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”‚
โ”‚  โ”‚ Code Module Table                          โ”‚  โ”‚
โ”‚  โ”‚  name โ†’ (offset, compressed_size,          โ”‚  โ”‚
โ”‚  โ”‚          uncompressed_size, is_c_ext)      โ”‚  โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ”‚
โ”‚  [4B: Asset Entry Count]                         โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”‚
โ”‚  โ”‚ Asset Table                                โ”‚  โ”‚
โ”‚  โ”‚  path โ†’ (offset, compressed_size,          โ”‚  โ”‚
โ”‚  โ”‚          uncompressed_size)                โ”‚  โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”‚
โ”‚  โ”‚ Single Zstd Block (all entries batched)    โ”‚  โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

4. RAM-Mapped Execution

  • The Rust bootloader memory-maps itself via memmap2, locates the VFS by its BEND footer signature, decompresses the single Zstd block, and passes the payload to the embedded Python runtime.
  • Before Py_Initialize(), VFS .pyc entries are extracted to a temp directory so importlib can find them during stdlib bootstrapping.
  • A BendMemoryFinder is injected into sys.meta_path[0] per PEP 451 โ€” all subsequent imports resolve from RAM.
  • C-extensions are written to anonymous memfd_create (Linux) or temp files (Windows) and loaded via dlopen/LoadLibrary with RTLD_GLOBAL.

5. Tree-Shaking, Incremental Cache & Debug Stripping

  • Tree-shaken stdlib: Modulefinder traces only the modules your app actually imports โ€” ~300โ€“400 modules instead of the full stdlib (~3,400).
  • Incremental cache: Bytecode is cached by (path, mtime, opt_level) in dist/.pybend_cache/, so rebuilds recompile only changed files.
  • Debug stripping: co_linetable/co_lnotab is stripped from all code objects, saving ~10% binary size.
  • Compression: All entry payloads batched into a single Zstd block (level 7 default, configurable 1โ€“19).

๐Ÿ“ฆ Quick Start

1. Install

pip install pybend

2. Zero-Config Build

If your project directory contains main.py, app.py, or manage.py (Django):

pybend build

That's it. The output lands at ./dist/<name>.bin (Linux) or ./dist/<name>.exe (Windows).

Django projects are auto-detected: --noreload is appended to runserver, project packages are force-included, and certifi.where() is patched for the VFS CA bundle. FastAPI projects detected via uvicorn.run() get the same treatment.

3. Explicit Build

pybend build --entry src/server.py --output deploy/server -O 2

4. Config-Driven Build (pybend.toml)

[build]
entry_point = "src/main.py"
output_exe = "dist/app"    # auto-appends .exe on Windows
optimization_level = 2
compress_level = 7         # Zstd compression level (1โ€“19)
strip_debug = true         # Strip co_linetable from bytecode
include_modules = ["hidden_dep"]
exclude_modules = ["test", "unittest"]  # or ["*"] to skip site-packages
include_data = ["config.json", "assets/"]
pybend build

5. Inspect a Compiled Binary

pybend inspect dist/app

Prints a table of all code and asset entries in the VFS โ€” names, compressed/uncompressed sizes, compression ratios, and module types.

6. Build Bootloader from Source

If the pre-built bootloader doesn't match your Python version:

pybend bootstrap

Requires Rust/Cargo. The built template is automatically staged for future use.

7. Fetching Embedded Assets at Runtime

Any file listed in include_data can be streamed from memory:

import pybend

config = pybend.get_asset("config.json")
template = pybend.get_asset("templates/email.html")

No disk access. No extraction. Directly from the VFS.


๐Ÿ—๏ธ Project Structure

pybend/
โ”œโ”€โ”€ pyproject.toml              # Hatchling build config
โ”œโ”€โ”€ bootloader/                 # Rust runtime engine
โ”‚   โ”œโ”€โ”€ Cargo.toml
โ”‚   โ””โ”€โ”€ src/
โ”‚       โ”œโ”€โ”€ main.rs
โ”‚       โ”œโ”€โ”€ extractor.rs
โ”‚       โ””โ”€โ”€ bootstrapper.rs
โ”œโ”€โ”€ pybend/                     # Python build orchestration
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”œโ”€โ”€ cli.py                  # Click CLI
โ”‚   โ”œโ”€โ”€ compiler.py             # Pipeline orchestrator
โ”‚   โ”œโ”€โ”€ config.py               # TOML + CLI config resolution
โ”‚   โ”œโ”€โ”€ detector.py             # Django/FastAPI auto-detection
โ”‚   โ”œโ”€โ”€ inspector.py            # Binary VFS table parser
โ”‚   โ”œโ”€โ”€ dependency_resolver.py  # Import tracing + filtering
โ”‚   โ”œโ”€โ”€ importer.py             # Runtime VFS finder & loaders
โ”‚   โ”œโ”€โ”€ splicer.py              # Binary tail-splicing
โ”‚   โ”œโ”€โ”€ vfs_builder.py          # Zstd batch VFS builder + cache
โ”‚   โ””โ”€โ”€ templates/              # Pre-built bootloader binaries
โ”œโ”€โ”€ docs/                       # MkDocs documentation
โ”œโ”€โ”€ tests/                      # Test suite
โ””โ”€โ”€ .github/workflows/          # CI/CD pipelines

โš™๏ธ Configuration Reference

CLI Commands

Command Description
build Compile a Python app into a standalone binary
bootstrap Build the bootloader template from Rust source
inspect Inspect the VFS table inside a compiled binary

CLI Flags (build)

Flag Shorthand Description
--entry -e Entry point script path
--output -o Output executable path
--optimize -O Optimization level (0, 1, 2)
--include -i Force-include a module
--exclude -x Exclude a module
--template -t Custom bootloader binary
--config -c Custom pybend.toml path
--target -p Target platform (os/arch, e.g. linux/x86_64)
--verbose -v Show detailed build output
--quiet -q Suppress all non-error output
--no-strip Do not strip debug symbols from output
--compress-level Zstd compression level (1โ€“19, default 7)
--no-strip-debug Keep co_linetable in bytecode (larger binary)

pybend.toml Fields

Field Type Default Description
entry_point string auto-discover Entry script path
output_exe string dist/<name>.bin/.exe Output binary path
optimization_level int 2 Bytecode optimization (0/1/2)
compress_level int 7 Zstd compression level (1โ€“19)
strip_debug bool true Strip co_linetable from bytecode
include_modules string[] [] Force-include modules
exclude_modules string[] [] Exclude modules (["*"] skips all site-packages)
include_data string[] [] Static file paths to embed

๐Ÿงช Testing

# Python test suite
python -m unittest discover -s tests -v

# Rust tests
cd bootloader && cargo test

Current test coverage: 49 Python tests + 2 Rust tests โ€” all passing.

Includes end-to-end compilation + execution tests, C-extension loading (memfd_create on Linux, .pyd+.dll on Windows), VFS structural validation, latency benchmarks, and disk-isolation verification.


๐ŸŽฏ Target Platforms

  • Linux x86_64 (primary, production-tested)
  • Windows x86_64 (Python 3.10โ€“3.14, CI-tested, .exe output with .pyd/.dll support)

Future targets: aarch64-unknown-linux-musl, aarch64-apple-darwin.


๐Ÿ“œ License

MIT โ€” see LICENSE.

Copyright (c) 2026 Jude Nii Klemesu Commey

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

pybend-1.0.1-py3-none-any.whl (53.2 MB view details)

Uploaded Python 3

File details

Details for the file pybend-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: pybend-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 53.2 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pybend-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c34082872d7bcc1bd3d26a828d150f7e9be8098adf173a9bd351c1341986d854
MD5 3cb76f069f71684a5a511f97493efbb4
BLAKE2b-256 e2023c48c5ca95bdc622c96c3d11c980ef6389f50f4dd5a9abe3d6841504a269

See more details on using hashes here.

Provenance

The following attestation bundles were made for pybend-1.0.1-py3-none-any.whl:

Publisher: release.yml on niicommey01/pybend

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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