Before you begin, ensure you have the following installed:
Python
Rust and Cargo (Rust's package manager)
Maturin (for building and publishing Rust crates as Python packages)
Step 1: Setting Up Environment
Create a Virtual Environment:
python3 -m venv .env
source .env/bin/activate
Step 2: Installing Maturin
pip install maturin
Inside the directory, now run maturin init. This will generate the new package source. When given the choice of bindings to use, select pyo3 bindings:
$ maturin init
β π€· Which kind of bindings to use?
π Documentation: https://maturin.rs/bindings.html Β· pyo3
β¨ Done! Initialized project /your/path
Step 3: Building and Installing the Python Module
This will build the package and install it into the Python virtualenv previously created and activated. The package is then ready to be used from python:
maturin develop
An example output would be:
π¦ Including license file "/path/to/project/LICENSE"
π Found pyo3 bindings
π Found CPython 3.12 at /path/to/project/.env/bin/python
π‘ Using build options features from pyproject.toml
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.21s
π¦ Built wheel for CPython 3.12 to /var/folders/.../algebraic_simplification_cesar-0.1.0-cp312-cp312-macosx_10_12_x86_64.whl
βοΈ Setting installed package as editable
π Installed algebraic_simplification_cesar-0.1.0
Step 4: Using the Python Module
Open a Python Shell:
Before you begin, ensure you have the following installed:
Step 1: Setting Up Environment Create a Virtual Environment:
Step 2: Installing Maturin
Inside the directory, now run
maturin init
. This will generate the new package source. When given the choice of bindings to use, select pyo3 bindings:Step 3: Building and Installing the Python Module This will build the package and install it into the Python virtualenv previously created and activated. The package is then ready to be used from
python
:An example output would be:
Step 4: Using the Python Module Open a Python Shell:
Import and Use the Module:
References For more detailed information on using PyO3, refer to the official PyO3 guide.