gitmyhub

pyo3

Rust ★ 16k updated 2h ago

Rust bindings for the Python interpreter

PyO3 lets you write fast Rust code and call it from Python as if it were a normal Python library, or embed Python inside a Rust program, combining Python's ease of use with Rust's speed.

RustPythonsetup: moderatecomplexity 3/5

PyO3 is a library that connects two programming languages — Rust and Python — letting you use them together in the same project. It works in two directions: you can write code in Rust and call it from Python (making Python faster for performance-critical parts), or you can run Python code from inside a Rust program (embedding the Python interpreter).

Rust is a systems programming language known for being very fast and memory-safe, while Python is popular for its ease of use. PyO3 lets you get the best of both: write the fast or complex parts in Rust, and expose them as if they were ordinary Python functions that any Python user can call. This is how many high-performance Python libraries are built under the hood.

The code example in the README shows how to write a Rust function, annotate it with a simple attribute (#[pyfunction]), and then call it from Python with a normal import statement — the compiled Rust code becomes a native Python module. Going the other direction, you can embed a Python interpreter inside a Rust application and call Python libraries from Rust code.

The companion tool maturin handles building and publishing these mixed Rust-Python packages with minimal configuration — you run a few commands and get a Python package you can distribute via pip (Python's package manager).

You would use PyO3 if you have a Python project with a performance bottleneck that you want to rewrite in Rust, or if you are building a Rust application that needs to use Python libraries. The full README is longer than what was provided.

Where it fits