PyO3 lets Rust code expose native Python modules or embed a Python interpreter in Rust binaries. It supports CPython, PyPy, and GraalPy versions listed in the docs.
Overview
PyO3 provides Rust crates and macros for two main paths: implementing Python extension modules in Rust, and running Python from within a Rust executable. It sits on the Python C API and is the binding layer many Rust Python packages build on.
For Python importers, typical workflows use maturin, setuptools-rust, or manual builds to produce wheels or editable installs. Developers define pymodule and pyfunction exports in Rust, then install into a virtual environment for import tests. Release builds use optimized maturin develop flags when benchmarking.
For Rust binaries, you enable auto-initialize features, link against a Python shared library, and call Python::attach to import modules and evaluate code. Ubuntu and RPM families need development packages installed so libpython is available at link time.
Supported interpreters include CPython 3.9+, PyPy 7.3 with Python 3.11+, and GraalPy 25.4+ on Python 3.13+. The user guide and API docs cover type conversions, async bridges, and ecosystem helpers such as numpy and serde integrations maintained as separate crates.
Key features
- Native Python modules from Rust
- Embed Python in Rust binaries
- maturin-friendly module layout
- CPython, PyPy, GraalPy support
- pyfunction and pymodule macros
- Python C API safe wrappers
- setuptools-rust compatible builds
- Ecosystem crates for numpy and async
Use cases
- High-performance Python extensions
- Rust apps scripting with Python
- Publishing Rust crates as pip wheels
- Mixing Python ML with Rust services