How can I separate a rust library and the pyo3 exported python extensions which wrap it

2024/11/15 15:51:46

I have a rust library which provides useful functionality for use in other rust programs. Additionally I would like to provide this functionality as a python extension (using pyo3 and setuptools-rust, although most of this is tool agnostic)

The documentation examples all show a single library. This means that anyone building the rust library and using it in the rust ecosystem needs the python headers and gets the exported python module as well.

How can I separate this as an independent rust library offering the functionality in rust and a python package offering the functionality in python?

Answer

After some experimentation I have arrived on the following setup which makes use of rust workspaces:

Directory structure:

FizzBuzz
- fizzbuzz- src- lib.rs- tests- test_....rs...- Cargo.toml
- fizzbuzzo3- src- lib.rs- Cargo.toml
- fizzbuzzpy- fizzbuzzpy- __init__.py- fizzbuzzer.py- tests- test_fizzbuzzo3.py- test_fizzbuzzpy.py
- Cargo.toml
- pyproject.toml

Where:

  • fizzbuzz contains the pure rust implementation
  • fizzbuzzo3 contains the pyo3 wrapped version for python
  • fizzbuzzpy contains additional native python functionality and pytest tests

Config files:

fizzbuzz/Cargo.toml
[package]
name = "fizzbuzz"
version = "0.2.1"
edition = "2021"[lib]
name = "fizzbuzz"
path = "src/lib.rs"
crate-type = ["rlib"]  # cdylib required for python import, rlib required for rust tests.
fizzbuzzo3/Cargo.toml
[package]
name = "fizzbuzzo3"
version = "0.1.0"
edition = "2021"[lib]
name = "fizzbuzzo3"
path = "src/lib.rs"
crate-type = ["cdylib"]  # cdylib required for python import, rlib required for rust tests.[dependencies]
pyo3 = { version = "0.21.0-beta.0" }
fizzbuzz = { path = "../fizzbuzz" }
./Cargo.toml
[workspace]members = ["fizzbuzz","fizzbuzzo3"
]resolver = "2"
pyproject.toml
[build-system]
requires = ["setuptools", "setuptools-rust"]
build-backend = "setuptools.build_meta"[project]
name = "fizzbuzz"
description = "An implementation of the traditional fizzbuzz kata"
...
[tool.setuptools.packages.find]
where = ["fizzbuzzpy"][[tool.setuptools-rust.ext-modules]]
# Private Rust extension module to be nested into the Python package
target = "fizzbuzzo3"           # The last part of the name (e.g. "_lib") has to match lib.name in Cargo.toml,# but you can add a prefix to nest it inside of a Python package.
path = "fizzbuzzo3/Cargo.toml"  # Default value, can be omitted
binding = "PyO3"                # Default value, can be omitted[project.optional-dependencies]
dev = ["black","pytest","pytest-doctest-mkdocstrings","ruff",]

How To:

  • build and test the pure rust implementation: ~/Fizzbuzz/fizzbuzz$ cargo test -v
  • build and unit test the python bindings: ~/Fizzbuzz/fizzbuzzo3$ cargo test -v
  • run all the rust tests: ~/Fizzbuzz$ cargo test -v
  • build, install and test the rust bindings in python: (.venv) ~/Fizzbuzz$ pip install -e .[dev] && pytest

The full codebase is at: MusicalNinjaDad/FizzBuzz if you want to look in more detail.

https://en.xdnf.cn/q/119643.html

Related Q&A

how do I count unique words of text files in specific directory with Python? [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

Python convert path to dict

I have a list of paths that need to be converted to a dict ["/company/accounts/account1/accountId=11111","/company/accounts/account1/accountName=testacc","/company/accounts/acc…

Python: How to download images with the URLs in the excel and replace the URLs with the pictures?

As shown in the below picture,theres an excel sheet and about 2,000 URLs of cover images in the F column. What I want to do is that downloading the pictures with the URLs and replace the URL with the…

I cant figure out pip tensorrt line 17 error

I couldnt install it in any way, I wonder what could be the cause of the error. I installed C++ and other necessary stuff I am using windows 11 I installed pip install nvidia-pyindex with no problem. S…

Extracting specific values for a header in different lines using regex

I have text string which has multiple lines and each line has mix of characters/numbers and spaces etc. Here is how a couple lines look like:WEIGHT VOLUME CHA…

Creating a function to process through a .txt file of student grades

Can someone help...My driver file is here:from functions import process_marks def main():try:f = open(argv[1])except FileNotFoundError:print("\nFile ", argv[1], "is not available")e…

Python Reddit PRAW get top week. How to change limit?

I have been familiarising myself with PRAW for reddit. I am trying to get the top x posts for the week, however I am having trouble changing the limit for the "top" method. The documentatio…

I want to convert string 1F to hex 1F in Python, what should I do?

num="1F" nm="1" nm1="2" hex(num)^hex(nm)^hex(nm1)I wrote it like the code above, but hex doesnt work properly. I want to convert the string to hexadecimal, and I want an x…

How to call a function in a Django template?

I have a function on my views.py file that connects to a mail server and then appends to my Django model the email addresses of the recipients. The script works good. In Django, Im displaying the model…

Remove duplicates from json file matching against multiple keys

Original Post = Remove duplicates from json dataThis is only my second post. I didnt have enough points to comment my question on the original post...So here I am.Andy Hayden makes a great point - &quo…