Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/willcrichton/cargo-single-pyo3
Generate a Python module from a single Rust file.
https://github.com/willcrichton/cargo-single-pyo3
Last synced: 2 months ago
JSON representation
Generate a Python module from a single Rust file.
- Host: GitHub
- URL: https://github.com/willcrichton/cargo-single-pyo3
- Owner: willcrichton
- License: mit
- Created: 2021-05-03T17:10:36.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2023-02-21T23:32:49.000Z (almost 2 years ago)
- Last Synced: 2024-09-19T04:18:36.596Z (3 months ago)
- Language: Rust
- Size: 16.6 KB
- Stars: 8
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# cargo-single-pyo3 ![crates.io](https://img.shields.io/crates/v/cargo-single-pyo3.svg)
Utility to build Python modules from a single Rust files via [pyo3](https://github.com/PyO3/pyo3). Inspired by [cargo-single](https://github.com/inejge/cargo-single).
## Installation
```
cargo install cargo-single-pyo3
```## Example
First, create a single Rust file with a pyo3 module. Add any dependencies as double-slash comments at the top of the file. For example, if you create `foo.rs` with the contents:
```rust
// rand = "*"use pyo3::prelude::*;
use pyo3::wrap_pyfunction;#[pyfunction]
fn sum_as_string(a: usize, b: usize) -> PyResult {
// using rand to demonstrate library support
let c = rand::thread_rng().gen_range(0 ..= 1);
Ok((a + b + c).to_string())
}#[pymodule]
fn foo(py: Python, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(sum_as_string, m)?)?;
Ok(())
}
```Then run:
```
cargo single-pyo3 foo.rs
```This should generate a file `foo.so`, which you can then import:
```
$ python3
>>> import foo
>>> foo.sum_as_string(1, 2)
'3'
>>> foo.sum_as_string(2, 2)
'5'
```## Usage notes
**Module name:** the name of the file is the name of the module, e.g. `foo.rs` generates `foo.so`. The name of the `#[pymodule]` function must be the same.
**Build process:** the tool creates a Cargo project in your temporary directory that is associated with the module name, e.g. `/tmp/foo`. This could cause any usual problems of conflicts between users or projects on the same machine, so be careful (or submit a PR if you have a different preference).
**Pyo3 version:** the Cargo dependency on pyo3 is automatically generated. If you need to change the version, use the `--pyo3` flag, e.g. `--pyo3 0.13`. You can also use `--pyo3 github` to use the latest on main branch. As of 5/7/21, the github option was necessary to build on OS X.