How to write Python extensions in Rust with PyO3

Every programming language has strengths and weaknesses. Python offers many convenient programming conventions but is computationally slow. Rust gives you machine-level speed and strong memory safety but is more complex than Python. The good news is, you can combine the two languages, wielding Python’s ease of use to harness Rust’s speed and power. The PyO3 project lets you leverage the best of both worlds by writing Python extensions in Rust.

With PyO3, you write Rust code, indicate how it interfaces with Python, then compile Rust and deploy it directly into a Python virtual environment, where you can use it unobtrusively with your Python code.

This article is a quick tour of how PyO3 works. You’ll learn how to set up a Python project with a PyO3 create, how to expose Rust functions as a Python module, and how to create Python objects like classes and exceptions in Rust.

Setting up a Python project with PyO3

To start creating a PyO3 project, you need to begin with a Python virtual environment, or venv. This is not just for the sake of having your Python project organized, but also to provide a place to install the Rust crate you’ll be building with PyO3. (If you haven’t already installed the Rust toolchain, do that now.)

The exact organization of the project directories can vary. In the examples shown in PyO3’s documentation, the PyO3 project is built in a directory that contains the Python project and its virtual environment. Another approach is to create two subdirectories: one for your Python project and its venv, and the other for the PyO3 project. The latter approach makes it easier to keep things organized, so we’ll do that:

  1. Create a new directory to hold both your Python and Rust projects. We’ll call them pyexample and rustexample, respectively.
  2. In the pyexample directory, create your virtual environment and activate it. We’ll eventually add some Python code here. It’s important that you perform all your work with both the Rust and Python code in your activated venv.
  3. In your activated venv, install the maturin package with pip install maturin. maturin is the tool we use to build our Rust project and integrate it with our Python project.
  4. Switch to the Rust project directory and type maturin init. When asked what bindings to select, choose pyo3.
  5. maturin will then generate a Rust project in that directory, complete with a Cargo.toml file that describes the project. Note that the project will be given the same name as the directory it’s placed in; in this case it’ll be rustexample.

Rust functions in a PyO3 project

When you create a PyO3 project’s scaffolding with maturin, it auto-creates a code stub file in src/lib.rs. This stub contains code for two functions—a single sample function, sum_as_string, and a function named after your project that exposes other functions as a Python module.

Copyright © 2023 IDG Communications, Inc.

Source link

Leave a Reply

Your email address will not be published. Required fields are marked *