Crate pyo3

source ·
Expand description

Rust bindings to the Python interpreter.

PyO3 can be used to write native Python modules or run Python code and modules from Rust.

See the guide for a detailed introduction.

§PyO3’s object types

PyO3 has several core types that you should familiarize yourself with:

§The Python<’py> object

Holding the global interpreter lock (GIL) is modeled with the Python<'py> token. All APIs that require that the GIL is held require this token as proof that you really are holding the GIL. It can be explicitly acquired and is also implicitly acquired by PyO3 as it wraps Rust functions and structs into Python functions and objects.

§The GIL-dependent types

For example &PyAny. These are only ever seen as references, with a lifetime that is only valid for as long as the GIL is held, which is why using them doesn’t require a Python<'py> token. The underlying Python object, if mutable, can be mutated through any reference.

See the guide for an explanation of the different Python object types.

§The GIL-independent types

When wrapped in Py<...>, like with Py<PyAny> or Py<SomePyClass>, Python objects no longer have a limited lifetime which makes them easier to store in structs and pass between functions. However, you cannot do much with them without a Python<'py> token, for which you’d need to reacquire the GIL.


The vast majority of operations in this library will return PyResult<...>. This is an alias for the type Result<..., PyErr>.

A PyErr represents a Python exception. A PyErr returned to Python code will be raised as a Python exception. Errors from PyO3 itself are also exposed as Python exceptions.

§Feature flags

PyO3 uses feature flags to enable you to opt-in to additional functionality. For a detailed description, see the Features chapter of the guide.

§Default feature flags

The following features are turned on by default:

  • macros: Enables various macros, including all the attribute macros.

§Optional feature flags

The following features customize PyO3’s behavior:

  • abi3: Restricts PyO3’s API to a subset of the full Python API which is guaranteed by PEP 384 to be forward-compatible with future Python versions.
  • auto-initialize: Changes Python::with_gil to automatically initialize the Python interpreter if needed.
  • extension-module: This will tell the linker to keep the Python symbols unresolved, so that your module can also be used with statically linked Python interpreters. Use this feature when building an extension module.
  • multiple-pymethods: Enables the use of multiple #[pymethods] blocks per #[pyclass]. This adds a dependency on the inventory crate, which is not supported on all platforms.

The following features enable interactions with other crates in the Rust ecosystem:

§Unstable features

  • nightly: Uses #![feature(auto_traits, negative_impls)] to define Ungil as an auto trait.

§rustc environment flags

PyO3 uses rustc’s --cfg flags to enable or disable code used for different Python versions. If you want to do this for your own crate, you can do so with the pyo3-build-config crate.

  • Py_3_7, Py_3_8, Py_3_9, Py_3_10: Marks code that is only enabled when compiling for a given minimum Python version.
  • Py_LIMITED_API: Marks code enabled when the abi3 feature flag is enabled.
  • PyPy - Marks code enabled when compiling for PyPy.

§Minimum supported Rust and Python versions

PyO3 supports the following software versions:

  • Python 3.7 and up (CPython and PyPy)
  • Rust 1.56 and up

§Example: Building a native Python module

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:


name = "string-sum"
version = "0.1.0"
edition = "2021"

name = "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"]
crate-type = ["cdylib"]

version = "0.21.0-dev"
features = ["extension-module"]


use pyo3::prelude::*;

/// Formats the sum of two numbers as string.
fn sum_as_string(a: usize, b: usize) -> PyResult<String> {
    Ok((a + b).to_string())

/// A Python module implemented in Rust.
fn string_sum(py: Python<'_>, m: &PyModule) -> PyResult<()> {
    m.add_function(wrap_pyfunction!(sum_as_string, m)?)?;


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:

$ cd string_sum
$ python -m venv .env
$ source .env/bin/activate
$ pip install maturin

Now build and execute the module:

$ maturin develop
# lots of progress output as maturin runs the compilation...
$ python
>>> import string_sum
>>> string_sum.sum_as_string(5, 20)

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.

§Example: Using Python from Rust

To embed Python into a Rust binary, you need to ensure that your Python installation contains a shared library. The following steps demonstrate how to ensure this (for Ubuntu), and then give some example code which runs an embedded Python interpreter.

To install the Python shared library on Ubuntu:

sudo apt install python3-dev

Start a new project with cargo new and add pyo3 to the Cargo.toml like this:

version = "0.21.0-dev"
# this is necessary to automatically initialize the Python interpreter
features = ["auto-initialize"]

Example program displaying the value of sys.version and the current user name:

use pyo3::prelude::*;
use pyo3::types::IntoPyDict;

fn main() -> PyResult<()> {
    Python::with_gil(|py| {
        let sys = py.import_bound("sys")?;
        let version: String = sys.getattr("version")?.extract()?;

        let locals = [("os", py.import_bound("os")?)].into_py_dict_bound(py);
        let code = "os.getenv('USER') or os.getenv('USERNAME') or 'Unknown'";
        let user: String = py.eval_bound(code, None, Some(&locals))?.extract()?;

        println!("Hello {}, I'm Python {}", user, version);

The guide has a section with lots of examples about this topic.

§Other Examples

The PyO3 README contains quick-start examples for both using Rust from Python and Python from Rust.

The PyO3 repository’s examples subdirectory contains some basic packages to demonstrate usage of PyO3.

There are many projects using PyO3 - see a list of some at




  • Add the module to the initialization table in order to make embedded Python code to use it. Module name is the argument.
  • Defines a new exception type.
  • Defines a Rust type for an exception defined in Python code.
  • Interns text as a Python string and stores a reference to it in static storage.
  • A convenient macro to execute a Python code snippet, with some local variables set.
  • Wraps a Rust function annotated with #[pyfunction].
  • Returns a function that takes a Python instance and returns a Python module.


  • A borrowed equivalent to Bound.
  • A GIL-attached equivalent to Py.
  • Error that indicates a failure to convert a PyAny to a more specific Python type.
  • Error that indicates a failure to convert a PyAny to a more specific Python type.
  • A RAII pool which PyO3 uses to store owned Python references.
  • A GIL-independent reference to an object allocated on the Python heap.
  • Error that indicates a failure to convert a PyAny to a more specific Python type.
  • Represents a Python exception.
  • Represents the major, minor, and patch (if any) versions of this interpreter.


  • Helper conversion trait that allows to use custom arguments for lazy exception construction.
  • Types that are built into the Python interpreter.


Type Aliases§

  • A commonly-used alias for Py<PyAny>.
  • Represents the result of a Python call.

Attribute Macros§

  • A proc macro used to expose Rust structs and fieldless enums as Python objects.
  • A proc macro used to expose Rust functions to Python.
  • A proc macro used to expose methods to Python.
  • A proc macro used to implement Python modules.

Derive Macros§