Generate a Python module from a single Rust file.

Related tags

cargo-single-pyo3
Overview

cargo-single-pyo3 crates.io

Utility to build Python modules from a single Rust files via pyo3. Inspired by 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:

PyResult { 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(()) } ">
// rand = "*"

use pyo3::prelude::*;
use pyo3::wrap_pyfunction;

#[pyfunction]
fn sum_as_string(a: usize, b: usize) -> PyResult<String> {
  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(1, 2)
'4'

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.

Owner
Will Crichton
Let's bring cognitive science to programming.
Will Crichton
Subcommand to show result of macro expansion

cargo-expand Once installed, the following command prints out the result of macro expansion and #[derive] expansion applied to the current crate. $ ca

David Tolnay 979 Jun 5, 2021
Webpack loader for Rust files. DEPRECATED, use WasmPack instead

The project is in low maintance now Use WasmPack instead Webpack Rust loader Webpack loader for Rust Example add.rs #[no_mangle] pub fn add(a: i32, b:

Max Eliseev 34 Feb 18, 2021
GitHub Actions for mdBook (rust-lang/mdBook) ⚡️ Setup mdBook quickly and build your site fast. Linux (Ubuntu), macOS, and Windows are supported.

GitHub Actions for mdBook rust-lang/mdBook Setup Action. We can run mdBook on a virtual machine of GitHub Actions by this mdBook action. Linux, macOS,

Shohei Ueda 131 Jun 8, 2021
Rust github action

Rust Github Action 'Silverbullet' for a quickstart Rust CI based upon Github Actions What's inside the "box": Rust 1.50.0 Rustfmt Clippy Cargo Release

Stefan Ruzitschka 55 Jun 17, 2021
An example project showing usage of CMake with Rust

An example of using CMake with Rust. Try it! mkdir build cd build cmake .. -DCMAKE_INSTALL_PREFIX=/tmp make -j make test -j make doc -j make install

null 94 Dec 29, 2020
Archer - a repository builder for ArchLinux

Archer - a repository builder for ArchLinux This project is at a very early stage. Current Progress Naive Dependency Resolving Todos dependency resolv

LightQuantum 9 Jun 5, 2021