Skip to main content

Python source bundler that produces a single .py file from multi-module projects

Project description

Cribo: Python Source Bundler

PyPI npm codecov Maintainability Rating License: MIT

Cribo is a Rust-based CLI tool that, via fast, heuristically proven bundling, consolidates a scattered Python codebase—from a single entry point or monorepo—into one idiomatic .py file. This not only streamlines deployment in environments like PySpark, AWS Lambda, and notebooks but also makes ingesting Python codebases into AI models easier and more cost-effective while preserving full functional insights.

What is "Cribo"?

Cribo is named after the Mussurana snake (Clelia clelia), nicknamed "Cribo" in Latin America. Just like the real Cribo specializes in hunting and neutralizing venomous snakes (with a diet that's 70-80% other snakes!), our tool wrangles Python dependencies and circular imports with ease. Brazilian farmers even keep Cribos around for natural pest control—think of this as the Python ecosystem's answer to dependency chaos. In short:Cribo eats tricky imports for breakfast, so your code doesn't have to!

Features

  • 🦀 Rust-based CLI based on Ruff's Python AST parser
  • 🐍 Can be installed via pip install cribo or npm install cribo
  • 😎 Contemporary minds can also use uvx cribo or bunx cribo
  • 🌲 Tree-shaking (enabled by default) to inline only the modules that are actually used
  • 🔄 Circular dependency resolution using Tarjan's strongly connected components (SCC) analysis and function-level lazy import transformations, with detailed diagnostics
  • 🧹 Unused import trimming to clean up Python files standalone
  • 📦 Requirements generation with optional requirements.txt output
  • 🔧 Configurable import classification and source directories
  • 🚀 Fast and memory-efficient

Reliability and Production Readiness

Cribo is built with production use cases in mind and is rigorously tested to ensure reliability and performance. You can confidently use it for production-grade code, backed by the following guarantees:

  • Comprehensive Test Suite: Cribo is continuously validated against a set of approximately 100 test fixtures that cover the full spectrum of Python's import system—from simple relative imports to complex scenarios involving circular dependencies and importlib constructs.

  • Real-World Ecosystem Testing: As part of every pull request, we run an "ecosystem" test suite. This involves bundling several popular open-source libraries (such as requests, httpx, pyyaml, idna, and rich) and executing test code against the resulting bundle to ensure real-world compatibility.

  • Performance Monitoring: We monitor microbenchmark regressions and ecosystem build time/size performance with every change. This ensures that Cribo's performance and efficiency are maintained and improved over time, preventing regressions from making their way into releases.

Installation

🔐 Supply Chain Security: All npm and pypi packages include provenance attestations for enhanced security and verification.

From PyPI (Python Package)

pip install cribo

From npm (Node.js CLI)

# Global installation
npm install -g cribo

# One-time use
bunx cribo --help

Binary Downloads

Download pre-built binaries for your platform from the latest release:

  • Linux x86_64: cribo_<version>_linux_x86_64.tar.gz
  • Linux ARM64: cribo_<version>_linux_arm64.tar.gz
  • macOS x86_64: cribo_<version>_darwin_x86_64.tar.gz
  • macOS ARM64: cribo_<version>_darwin_arm64.tar.gz
  • Windows x86_64: cribo_<version>_windows_x86_64.zip
  • Windows ARM64: cribo_<version>_windows_arm64.zip

Each binary includes a SHA256 checksum file for verification.

Package Manager Installation

Aqua

If you use Aqua, add to your aqua.yaml:

registries:
  - type: standard
    ref: latest
packages:
  - name: ophidiarium/cribo@latest

Then run:

aqua install

UBI (Universal Binary Installer)

Using UBI:

# Install latest version
ubi --project ophidiarium/cribo

# Install specific version
ubi --project ophidiarium/cribo --tag v0.4.1

# Install to specific directory
ubi --project ophidiarium/cribo --in /usr/local/bin

From Source

git clone https://github.com/ophidiarium/cribo.git
cd cribo
cargo build --release

Quick Start

Command Line Usage

# Basic bundling
cribo --entry src/main.py --output bundle.py

# Bundle a package directory (looks for __main__.py or __init__.py)
cribo --entry mypackage/ --output bundle.py

# Generate requirements.txt
cribo --entry src/main.py --output bundle.py --emit-requirements

# Verbose output (can be repeated for more detail: -v, -vv, -vvv)
cribo --entry src/main.py --output bundle.py -v
cribo --entry src/main.py --output bundle.py -vv    # debug level
cribo --entry src/main.py --output bundle.py -vvv   # trace level

# Custom config file
cribo --entry src/main.py --output bundle.py --config my-cribo.toml

CLI Options

  • -e, --entry <PATH>: Entry point Python script or package directory (required). When pointing to a directory, Cribo will look for __main__.py first, then __init__.py
  • -o, --output <PATH>: Output bundled Python file (required)
  • -v, --verbose...: Increase verbosity level. Can be repeated for more detail:
    • No flag: warnings and errors only
    • -v: informational messages
    • -vv: debug messages
    • -vvv or more: trace messages
  • -c, --config <PATH>: Custom configuration file path
  • --emit-requirements: Generate requirements.txt with third-party dependencies
  • --no-tree-shake: Disable tree-shaking optimization (tree-shaking is enabled by default)
  • --target-version <VERSION>: Target Python version (e.g., py38, py39, py310, py311, py312, py313)
  • -h, --help: Print help information
  • -V, --version: Print version information

The verbose flag is particularly useful for debugging bundling issues. Each level provides progressively more detail:

# Default: only warnings and errors
cribo --entry main.py --output bundle.py

# Info level: shows progress messages
cribo --entry main.py --output bundle.py -v

# Debug level: shows detailed processing steps
cribo --entry main.py --output bundle.py -vv

# Trace level: shows all internal operations
cribo --entry main.py --output bundle.py -vvv

The verbose levels map directly to Rust's log levels and can also be controlled via the RUST_LOG environment variable for more fine-grained control:

# Equivalent to -vv
RUST_LOG=debug cribo --entry main.py --output bundle.py

# Module-specific logging
RUST_LOG=cribo::bundler=trace,cribo::resolver=debug cribo --entry main.py --output bundle.py

Tree-Shaking

Tree-shaking is enabled by default to reduce bundle size by removing unused code:

# Bundle with tree-shaking (default behavior)
cribo --entry main.py --output bundle.py

# Disable tree-shaking to include all code
cribo --entry main.py --output bundle.py --no-tree-shake

How it works:

  • Analyzes your code starting from the entry point
  • Tracks which functions, classes, and variables are actually used
  • Removes unused symbols while preserving functionality
  • Respects __all__ declarations and module side effects
  • Preserves all symbols from directly imported modules (import module)

When to disable tree-shaking:

  • If you encounter undefined symbol errors with complex circular dependencies
  • When you need to preserve all code for dynamic imports or reflection
  • For debugging purposes to see the complete bundled output

Configuration

Cribo supports hierarchical configuration with the following precedence (highest to lowest):

  1. CLI-provided config (--config flag)
  2. Environment variables (with CRIBO_ prefix)
  3. Project config (cribo.toml in current directory)
  4. User config (~/.config/cribo/cribo.toml)
  5. System config (/etc/cribo/cribo.toml on Unix, %SYSTEMDRIVE%\ProgramData\cribo\cribo.toml on Windows)
  6. Default values

Configuration File Format

Create a cribo.toml file:

# Source directories to scan for first-party modules
src = ["src", ".", "lib"]

# Known first-party module names
known_first_party = [
    "my_internal_package",
]

# Known third-party module names
known_third_party = [
    "requests",
    "numpy",
    "pandas",
]

# Whether to preserve comments in the bundled output
preserve_comments = true

# Whether to preserve type hints in the bundled output
preserve_type_hints = true

# Target Python version for standard library checks
# Supported: "py38", "py39", "py310", "py311", "py312", "py313"
target-version = "py310"

Environment Variables

All configuration options can be overridden using environment variables with the CRIBO_ prefix:

# Comma-separated lists
export CRIBO_SRC="src,lib,custom_dir"
export CRIBO_KNOWN_FIRST_PARTY="mypackage,myotherpackage"
export CRIBO_KNOWN_THIRD_PARTY="requests,numpy"

# Boolean values (true/false, 1/0, yes/no, on/off)
export CRIBO_PRESERVE_COMMENTS="false"
export CRIBO_PRESERVE_TYPE_HINTS="true"

# String values
export CRIBO_TARGET_VERSION="py312"

Configuration Locations

  • Project: ./cribo.toml
  • User:
    • Linux/macOS: ~/.config/cribo/cribo.toml
    • Windows: %APPDATA%\cribo\cribo.toml
  • System:
    • Linux/macOS: /etc/cribo/cribo.toml or /etc/xdg/cribo/cribo.toml
    • Windows: %SYSTEMDRIVE%\ProgramData\cribo\cribo.toml

How It Works

  1. Module Discovery: Scans configured source directories to discover first-party Python modules
  2. Import Classification: Classifies imports as first-party, third-party, or standard library
  3. Dependency Graph: Builds a dependency graph and performs topological sorting
  4. Circular Dependency Resolution: Detects and intelligently resolves function-level circular imports
  5. Tree Shaking: Removes unused code by analyzing which symbols are actually used (enabled by default)
  6. Code Generation: Generates a single Python file with proper module separation
  7. Requirements: Optionally generates requirements.txt with third-party dependencies

Output Structure

The bundled output follows this structure:

#!/usr/bin/env python3
# Generated by Cribo - Python Source Bundler

# Preserved imports (stdlib and third-party)
import os
import sys
import requests

# ─ Module: utils/helpers.py ─
def greet(name: str) -> str:
    return f"Hello, {name}!"

# ─ Module: models/user.py ─
class User:
    def **init**(self, name: str):
        self.name = name

# ─ Entry Module: main.py ─
from utils.helpers import greet
from models.user import User

def main():
    user = User("Alice")
    print(greet(user.name))

if **name** == "**main**":
    main()

Use Cases

PySpark Jobs

Deploy complex PySpark applications as a single file:

cribo --entry spark_job.py --output dist/spark_job_bundle.py --emit-requirements
spark-submit dist/spark_job_bundle.py

AWS Lambda

Package Python Lambda functions with all dependencies:

cribo --entry lambda_handler.py --output deployment/handler.py
# Upload handler.py + requirements.txt to Lambda

Special Considerations

Pydantic Compatibility

Cribo preserves class identity and module structure to ensure Pydantic models work correctly:

# Original: models/user.py
class User(BaseModel):
    name: str


# Bundled output preserves **module** and class structure

Pandera Decorators

Function and class decorators are preserved with their original module context:

# Original: validators/schemas.py
@pa.check_types
def validate_dataframe(df: DataFrame[UserSchema]) -> DataFrame[UserSchema]:
    return df


# Bundled output maintains decorator functionality

Circular Dependencies

Cribo intelligently handles circular dependencies with advanced detection and resolution:

Resolvable Cycles (Function-Level)

Function-level circular imports are automatically resolved and bundled successfully:

# module_a.py
from module_b import process_b


def process_a():
    return process_b() + "->A"


# module_b.py
from module_a import get_value_a


def process_b():
    return f"B(using_{get_value_a()})"

Result: ✅ Bundles successfully with warning log

Unresolvable Cycles (Module Constants)

Temporal paradox patterns are detected and reported with detailed diagnostics:

# constants_a.py
from constants_b import B_VALUE

A_VALUE = B_VALUE + 1  # ❌ Unresolvable

# constants_b.py
from constants_a import A_VALUE

B_VALUE = A_VALUE * 2  # ❌ Temporal paradox

Result: ❌ Fails with detailed error message and resolution suggestions:

Unresolvable circular dependencies detected:

Cycle 1: constants_b  constants_a
  Type: ModuleConstants
  Reason: Module-level constant dependencies create temporal paradox - cannot be resolved through bundling

Comparison with Other Tools

Tool Language Tree Shaking Import Cleanup Circular Deps PySpark Ready Type Hints
Cribo Rust ✅ Default ✅ Smart Resolution
PyInstaller Python ❌ Fails
Nuitka Python ❌ Fails
Pex Python ❌ Fails

Contributing

Please see our Contributing Guidelines for details on how to contribute to the project.

License

This project uses a dual licensing approach:

What this means

  • For the source code: You can freely use, modify, and distribute the code for any purpose with minimal restrictions under the MIT license.
  • For the documentation: You can share, adapt, and use the documentation for any purpose (including commercially) as long as you provide appropriate attribution under CC BY 4.0.

See the LICENSE file for the MIT license text and docs/LICENSE for the CC BY 4.0 license text.

Acknowledgments

  • Ruff: Python AST parsing and import resolution logic inspiration
  • Maturin: Python-Rust integration

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

cribo-0.8.0.tar.gz (560.5 kB view details)

Uploaded Source

Built Distributions

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

cribo-0.8.0-py3-none-win_arm64.whl (2.6 MB view details)

Uploaded Python 3Windows ARM64

cribo-0.8.0-py3-none-win_amd64.whl (2.8 MB view details)

Uploaded Python 3Windows x86-64

cribo-0.8.0-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.9 MB view details)

Uploaded Python 3manylinux: glibc 2.17+ x86-64

cribo-0.8.0-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (2.6 MB view details)

Uploaded Python 3manylinux: glibc 2.17+ ARM64

cribo-0.8.0-py3-none-manylinux_2_5_x86_64.manylinux1_x86_64.whl (2.9 MB view details)

Uploaded Python 3manylinux: glibc 2.5+ x86-64

cribo-0.8.0-py3-none-macosx_11_0_arm64.whl (2.6 MB view details)

Uploaded Python 3macOS 11.0+ ARM64

cribo-0.8.0-py3-none-macosx_10_12_x86_64.whl (2.8 MB view details)

Uploaded Python 3macOS 10.12+ x86-64

File details

Details for the file cribo-0.8.0.tar.gz.

File metadata

  • Download URL: cribo-0.8.0.tar.gz
  • Upload date:
  • Size: 560.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for cribo-0.8.0.tar.gz
Algorithm Hash digest
SHA256 c381d781073075dd6badc7adb2e92ff91fa4ada802737364deacc0e1cc293e76
MD5 2092a641364ea43ebdf3de70db433361
BLAKE2b-256 80e4a5354d2994e79231a2433ae9ab20b72f8dda1432f00f8090ef85232207a3

See more details on using hashes here.

Provenance

The following attestation bundles were made for cribo-0.8.0.tar.gz:

Publisher: release.yml on ophidiarium/cribo

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

File details

Details for the file cribo-0.8.0-py3-none-win_arm64.whl.

File metadata

  • Download URL: cribo-0.8.0-py3-none-win_arm64.whl
  • Upload date:
  • Size: 2.6 MB
  • Tags: Python 3, Windows ARM64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for cribo-0.8.0-py3-none-win_arm64.whl
Algorithm Hash digest
SHA256 2b15fe11fb646f699ab536b8ca0fe181e3cc7c6d927c7843c605ba70d9f52b7b
MD5 69ed1db220545dfaffade09223def358
BLAKE2b-256 6e45cfe1bd041e257d395f9c81aed6f8f1fb2f7f43561a83a934dceb0c6b6cb9

See more details on using hashes here.

Provenance

The following attestation bundles were made for cribo-0.8.0-py3-none-win_arm64.whl:

Publisher: release.yml on ophidiarium/cribo

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

File details

Details for the file cribo-0.8.0-py3-none-win_amd64.whl.

File metadata

  • Download URL: cribo-0.8.0-py3-none-win_amd64.whl
  • Upload date:
  • Size: 2.8 MB
  • Tags: Python 3, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for cribo-0.8.0-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 282f57c3a3505ef2166658cfd22ff664b0e1b589781d489cbe91477345b81126
MD5 5726b4603aeed4c2cc1c5bd2dc5de528
BLAKE2b-256 2fb444f057341fa3a26e337af7f233ad4a20d7ae41c57b4fa159862291674300

See more details on using hashes here.

Provenance

The following attestation bundles were made for cribo-0.8.0-py3-none-win_amd64.whl:

Publisher: release.yml on ophidiarium/cribo

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

File details

Details for the file cribo-0.8.0-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for cribo-0.8.0-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a6b0c2f1c32c5b16c89247b7666b8f7ebde80463668eb3e6c24b6e099e4f58b8
MD5 9033c4106dc3333bcdd4221c25027b63
BLAKE2b-256 9e35e659045ee9bc8d6446b71c1aee770bd6c3eb9a7e98327267289aa51da5f7

See more details on using hashes here.

Provenance

The following attestation bundles were made for cribo-0.8.0-py3-none-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release.yml on ophidiarium/cribo

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

File details

Details for the file cribo-0.8.0-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for cribo-0.8.0-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 e4284f25ae9d83537277208ad22e82af6770238e7fc3ecd3f68cca5ff9969431
MD5 37a6af7880deb497b9c5ff8d1fe7f16b
BLAKE2b-256 fa86015d9ccc65f4810b6f67c436b7373f0314615bcb16cfbf135123597fb077

See more details on using hashes here.

Provenance

The following attestation bundles were made for cribo-0.8.0-py3-none-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: release.yml on ophidiarium/cribo

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

File details

Details for the file cribo-0.8.0-py3-none-manylinux_2_5_x86_64.manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for cribo-0.8.0-py3-none-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 6f9aa4710ad32feee490ef0b59c0b1b34e9620783dd0b7d9762c507a1e408251
MD5 b0e0e440297a04236f4fdb3f57877709
BLAKE2b-256 4c289924c4368701e9931a434371c3ed45e9055bbbce86add08f7a6090d2012e

See more details on using hashes here.

Provenance

The following attestation bundles were made for cribo-0.8.0-py3-none-manylinux_2_5_x86_64.manylinux1_x86_64.whl:

Publisher: release.yml on ophidiarium/cribo

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

File details

Details for the file cribo-0.8.0-py3-none-macosx_11_0_arm64.whl.

File metadata

  • Download URL: cribo-0.8.0-py3-none-macosx_11_0_arm64.whl
  • Upload date:
  • Size: 2.6 MB
  • Tags: Python 3, macOS 11.0+ ARM64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for cribo-0.8.0-py3-none-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 33e3286f71779e816648a9cb3db75f6c1fb73270cc020acce3e8a0fbb8cdd8b0
MD5 5ad44ba79f585e83d26f32cfc2a32098
BLAKE2b-256 be87efb49c8b33308559ac75d634e7533b40c724483e81cce556f82051186fa0

See more details on using hashes here.

Provenance

The following attestation bundles were made for cribo-0.8.0-py3-none-macosx_11_0_arm64.whl:

Publisher: release.yml on ophidiarium/cribo

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

File details

Details for the file cribo-0.8.0-py3-none-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for cribo-0.8.0-py3-none-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 42e789e749f7687a4d4cad39415b6ea6423120e35c15f172cd929527f18ea05f
MD5 e9c42ec1a369d43503d1e4592c7b56fc
BLAKE2b-256 d987eebd5e59f80982524de331c985400d91ef7b29ef825089b29837b6cc9b0c

See more details on using hashes here.

Provenance

The following attestation bundles were made for cribo-0.8.0-py3-none-macosx_10_12_x86_64.whl:

Publisher: release.yml on ophidiarium/cribo

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