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.
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!
- 🦀 Rust-based CLI based on Ruff's Python AST parser
- 🐍 Can be installed via
pip install criboornpm install cribo - 😎 Contemporary minds can also use
uvx criboorbunx 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.txtoutput - 🔧 Configurable import classification and source directories
- 🚀 Fast and memory-efficient
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
importlibconstructs. -
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, andrich) 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.
🔐 Supply Chain Security: All npm and pypi packages include provenance attestations for enhanced security and verification.
pip install cribo# Global installation
npm install -g cribo
# One-time use
bunx cribo --helpDownload 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.
If you use Aqua, add to your aqua.yaml:
registries:
- type: standard
ref: latest
packages:
- name: ophidiarium/cribo@latestThen run:
aqua installUsing 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/bingit clone https://github.com/ophidiarium/cribo.git
cd cribo
cargo build --release# 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-e, --entry <PATH>: Entry point Python script or package directory (required). When pointing to a directory, Cribo will look for__main__.pyfirst, 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-vvvor 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 -vvvThe 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.pyTree-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-shakeHow 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
Cribo supports hierarchical configuration with the following precedence (highest to lowest):
- CLI-provided config (
--configflag) - Environment variables (with
CRIBO_prefix) - Project config (
cribo.tomlin current directory) - User config (
~/.config/cribo/cribo.toml) - System config (
/etc/cribo/cribo.tomlon Unix,%SYSTEMDRIVE%\ProgramData\cribo\cribo.tomlon Windows) - Default values
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"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"- Project:
./cribo.toml - User:
- Linux/macOS:
~/.config/cribo/cribo.toml - Windows:
%APPDATA%\cribo\cribo.toml
- Linux/macOS:
- System:
- Linux/macOS:
/etc/cribo/cribo.tomlor/etc/xdg/cribo/cribo.toml - Windows:
%SYSTEMDRIVE%\ProgramData\cribo\cribo.toml
- Linux/macOS:
- Module Discovery: Scans configured source directories to discover first-party Python modules
- Import Classification: Classifies imports as first-party, third-party, or standard library
- Dependency Graph: Builds a dependency graph and performs topological sorting
- Circular Dependency Resolution: Detects and intelligently resolves function-level circular imports
- Tree Shaking: Removes unused code by analyzing which symbols are actually used (enabled by default)
- Code Generation: Generates a single Python file with proper module separation
- Requirements: Optionally generates
requirements.txtwith third-party dependencies
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()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.pyPackage Python Lambda functions with all dependencies:
cribo --entry lambda_handler.py --output deployment/handler.py
# Upload handler.py + requirements.txt to LambdaCribo 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 structureFunction 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 functionalityCribo intelligently handles circular dependencies with advanced detection and resolution:
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
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 paradoxResult: ❌ 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| 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 | ❌ | ✅ |
Please see our Contributing Guidelines for details on how to contribute to the project.
This project uses a dual licensing approach:
- Source Code: Licensed under the MIT License
- Documentation: Licensed under the Creative Commons Attribution 4.0 International License (CC BY 4.0)
- 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.
- Ruff: Python AST parsing and import resolution logic inspiration
- Maturin: Python-Rust integration