Generate a Python module from a single Rust file.

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.

You might also like...
Single-reader, multi-writer & single-reader, multi-verifier; broadcasts reads to multiple writeable destinations in parallel

Bus Writer This Rust crate provides a generic single-reader, multi-writer, with support for callbacks for monitoring progress. It also provides a gene

Handoff is an unbuffered, single-producer / single-consumer, async channel

handoff handoff is a single-producer / single-consumer, unbuffered, asynchronous channel. It's intended for cases where you want blocking communicatio

Python module implemented in Rust for counting the number of one bits in a numpy array.

bit-counter Package for counting the number of one bits in a numpy array of uint8 values. Implemented as a Python module using Rust, providing high pe

Pure rust implementation of python's random module with compatible generator behaviour.

pyrand Pure rust implementation of (parts of) python's random module with compatible PRNG behaviour: seeding with equivalent values will yield identic

Rust File Management CLI is a command-line tool written in Rust that provides essential file management functionalities. Whether you're working with files or directories, this tool simplifies common file operations with ease.

Rust FileOps Rust File Management CLI is a command-line tool written in Rust that provides essential file management functionalities. Whether you're w

Rust single-process scheduling. Ported from schedule for Python

Rust single-process scheduling. Ported from schedule for Python, in turn inspired by clockwork (Ruby), and "Rethinking Cron" by Adam Wiggins.

A simple to use rust package to generate or parse Twitter snowflake IDs,generate time sortable 64 bits unique ids for distributed systems

A simple to use rust package to generate or parse Twitter snowflake IDs,generate time sortable 64 bits unique ids for distributed systems (inspired from twitter snowflake)

A utility that can download JavaScript and TypeScript module graphs and store them locally in a special zip file.

eszip A utility that can download JavaScript and TypeScript module graphs and store them locally in a special zip file. To create a new archive: esz

⚡ Garry's Mod module that boosts performance by moving -condebug file I/O to a separate thread

This is a Garry's Mod server module that moves -condebug file I/O out of the main thread, which should significantly improve performance for noisy servers.

single file, std only, async Rust executor

whorl - A single file, std only, async Rust executor whorl was created to teach you how async executors work in Rust. It is not the fastest executor n

Rust library to scan files and expand multi-file crates source code as a single tree

syn-file-expand This library allows you to load full source code of multi-file crates into a single syn::File. Features: Based on syn crate. Handling

Shows how to implement USB device on RP2040 in Rust, in a single file, with no hidden parts.

Rust RP2040 USB Device Example This is a worked example of implementing a USB device on the RP2040 microcontroller, in Rust. It is designed to be easy

serve a static site, single page application or just a static file with Rust
serve a static site, single page application or just a static file with Rust

cargo-server tl;dr: Does the same as "python -m http.server" or "npx serve" but for Rust ecosystem. cargo-server helps you serve a static site, single

Serve a static site, single page application or just a static file with Rust
Serve a static site, single page application or just a static file with Rust

cargo-server tl;dr: Does the same as "python -m http.server" or "npx serve" but for Rust ecosystem. cargo-server helps you serve a static site, single

A command line tool for easily generating multiple versions of a configuration file from a single template

MultiConf A command line tool for easily generating multiple versions of a configuration file from a single template. Why? I'm a big fan of the i3 win

Set Shell Environment Variables across multiple shells with a single configuration file.

Xshe – Cross-Shell Environment Vars xshe allows for setting Shell Environment Variables across multiple shells with a single TOML configuration file.

Download a single file from a Git repository.

git-download Microservices architecture requires sharing service definition files like in protocol buffer, for clients to access the server. To share

A crate for converting an ASCII text string or file to a single unicode character

A crate for converting an ASCII text string or file to a single unicode character. Also provides a macro to embed encoded source code into a Rust source file. Can also do the same to Python code while still letting the code run as before by wrapping it in a decoder.

singe's grep - a fast grep using single-file parallelism

singrep singe's grep - a fast grep using single-file parallelism singrep makes use of deterministic kernel file cache'ing to read the file fast enough

Owner
Will Crichton
Let's bring cognitive science to programming.
Will Crichton
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 76 Dec 28, 2022
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 231 Jan 2, 2023
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 36 Jan 12, 2022
A simple library for use one Rust code in multi platforms such as WebAssembly, Android and iOS

chameleon chameleon is a simple library for use one Rust code in multi platforms such as WebAssembly, Android and iOS. Key Features TBA Quick Start TB

Chris Ohk 3 Oct 18, 2021
ttvm is a runtime and compiler infrastructure written in Rust.

ttvm - Runtime and compiler infrastructure ttvm is a runtime and compiler infrastructure written in Rust. Usage Add the following to your Cargo.toml:

maDeveloper 1 Apr 19, 2022
🦎 Prototypes on polymorphic, metamorphic and poly-metamorphic malwares in Rust 🦎

chameleon-rs Prototypes on polymorphic, metamorphic and poly-metamorphic malwares in Rust. Disclaimer This project is for educational purposes only. I

Quartz Technology 8 Nov 2, 2023
Single File Assets is a file storage format for images

SFA (Rust) Single File Assets is a file storage format for images. The packed images are not guaranteed to be of same format because the format while

null 1 Jan 23, 2022
🖼️Combines a Javascript and WASM file into a single executable polygot PNG+HTML file.

WASIMAGE Combines a Javascript and WASM file into a single executable polygot PNG+HTML file. Usage: cargo install wasimage wasimage --wasm-file my.was

Nervive 2 Mar 30, 2022
A Redis module that provides rate limiting in Redis as a single command.

redis-cell A Redis module that provides rate limiting in Redis as a single command. Implements the fairly sophisticated generic cell rate algorithm (G

Brandur Leach 1.1k Jan 6, 2023
A single-producer single-consumer Rust queue with smart batching

Batching Queue A library that implements smart batching between a producer and a consumer. In other words, a single-producer single-consumer queue tha

Roland Kuhn 2 Dec 21, 2021