pyo3-ffi
This crate provides Rust FFI declarations for Python 3. It supports both the stable and the unstable component of the ABI through the use of cfg flags. Python Versions 3.7+ are supported. It is meant for advanced users only - regular PyO3 users shouldn't need to interact with this crate at all.
The contents of this crate are not documented here, as it would entail basically copying the documentation from CPython. Consult the Python/C API Reference Manual for up-to-date documentation.
Minimum supported Rust and Python versions
Requires Rust 1.63 or greater.
pyo3-ffi
supports the following Python distributions:
- CPython 3.7 or greater
- PyPy 7.3 (Python 3.9+)
- GraalPy 24.0 or greater (Python 3.10+)
Example: Building Python Native modules
PyO3 can be used to generate a native Python module. The easiest way to try this out for the
first time is to use maturin
. maturin
is a tool for building and publishing Rust-based
Python packages with minimal configuration. The following steps set up some files for an example
Python module, install maturin
, and then show how to build and import the Python module.
First, create a new folder (let's call it string_sum
) containing the following two files:
Cargo.toml
[]
= "string_sum"
# "cdylib" is necessary to produce a shared library for Python to import from.
#
# Downstream Rust code (including code in `bin/`, `examples/`, and `tests/`) will not be able
# to `use string_sum;` unless the "rlib" or "lib" crate type is also included, e.g.:
# crate-type = ["cdylib", "rlib"]
= ["cdylib"]
[]
= "0.23.1"
= ["extension-module"]
[]
# This is only necessary if you need to configure your build based on
# the Python version or the compile-time configuration for the interpreter.
= "0.23.1"
If you need to use conditional compilation based on Python version or how
Python was compiled, you need to add pyo3-build-config
as a
build-dependency
in your Cargo.toml
as in the example above and either
create a new build.rs
file or modify an existing one so that
pyo3_build_config::use_pyo3_cfgs()
gets called at build time:
build.rs
src/lib.rs
use ;
use ptr;
use *;
static mut MODULE_DEF: PyModuleDef = PyModuleDef ;
static mut METHODS: & = &;
// The module initialization function, which must be named `PyInit_<your_module>`.
pub unsafe extern "C"
/// A helper to parse function arguments
/// If we used PyO3's proc macros they'd handle all of this boilerplate for us :)
unsafe
unsafe
pub unsafe extern "C"
With those two files in place, now maturin
needs to be installed. This can be done using
Python's package manager pip
. First, load up a new Python virtualenv
, and install maturin
into it:
Now build and execute the module:
# lots of progress output as maturin runs the compilation...
>>> import
>>> string_sum.sum_as_string()
As well as with maturin
, it is possible to build using setuptools-rust or
manually. Both offer more flexibility than maturin
but require further
configuration.
While most projects use the safe wrapper provided by PyO3,
you can take a look at the orjson
library as an example on how to use pyo3-ffi
directly.
For those well versed in C and Rust the tutorials from the CPython documentation
can be easily converted to rust as well.