Introduction
RustPython is a Python 3 interpreter written entirely in Rust. It aims for full CPython compatibility while offering memory safety, easy embedding into Rust applications, and the ability to compile to WebAssembly for browser and edge environments.
What RustPython Does
- Interprets Python 3 code with a bytecode compiler and virtual machine written in Rust
- Compiles to WebAssembly so Python can run in browsers without server-side processing
- Embeds into Rust applications as a scripting engine via a clean Rust API
- Implements the Python standard library progressively, covering core modules like os, json, and re
- Passes a growing portion of the CPython test suite to ensure compatibility
Architecture Overview
RustPython uses a multi-phase pipeline: a parser converts Python source into an AST, a compiler transforms the AST into bytecode, and a stack-based virtual machine executes it. The VM manages Python objects through Rust's ownership model, avoiding a global interpreter lock. Each Python object is a Rust struct implementing a PyValue trait, and the garbage collector leverages Rust's reference counting with cycle detection.
Self-Hosting & Configuration
- Build from source with
cargo build --releaseor install viacargo install - Use
--target wasm32-wasifor WebAssembly builds - Embed by adding
rustpython-vmas a Cargo dependency in your Rust project - Configure via standard Python mechanisms (PYTHONPATH, site-packages)
- No external runtime dependencies; single static binary output
Key Features
- Memory-safe Python execution without a GIL, enabling true parallelism potential
- WebAssembly compilation for running Python in browsers and serverless environments
- Clean embedding API for adding Python scripting to Rust applications
- No C dependencies in the core interpreter, simplifying cross-compilation
- Active development tracking CPython 3.12+ compatibility
Comparison with Similar Tools
- CPython — the reference implementation with full ecosystem support; RustPython trades completeness for safety and portability
- PyPy — JIT-compiled Python for speed; RustPython focuses on embedding and WASM rather than raw performance
- Pyodide — CPython compiled to WASM via Emscripten; RustPython is a native Rust rewrite rather than a port
- MicroPython — targets microcontrollers with a minimal subset; RustPython aims for full Python 3 compatibility
- GraalPython — runs on GraalVM/JVM; RustPython produces standalone native or WASM binaries
FAQ
Q: Can RustPython run my existing Python projects? A: It depends on the project's dependencies. Pure Python code generally works, but C extension modules (numpy, pandas) are not supported.
Q: Is RustPython faster than CPython? A: Not currently for most workloads. The focus is on correctness and portability rather than raw speed.
Q: Can I use pip with RustPython? A: Basic pip support exists, but packages with C extensions will not install. Pure Python packages work.
Q: What Python version does it target? A: RustPython targets Python 3.12+ syntax and semantics, with ongoing work to close compatibility gaps.