Questions tagged [pyo3]

Used for questions related to the Rust library pyo3.

pyo3 is a Rust library that provides bindings for Python.

161 questions
14
votes
1 answer

How to call a Rust function from a Python file using pyo3?

I'm working on a videogame in which I'll need to set Rust objects (like, add a button with texture:"", coords:"", text:"", action:"") from Python files. I'm using the pyo3 crate to link Python and Rust. I succeed calling Python scripts from my Rust…
Suraii
  • 141
  • 3
11
votes
1 answer

How can I access a Rust Iterator from Python using PyO3?

I'm quite new with Rust, and my first 'serious' project has involved writing a Python wrapper for a small Rust library using PyO3. This has mostly been quite painless, but I'm struggling to work out how to expose lazy iterators over Rust Vecs to…
thesketh
  • 319
  • 2
  • 7
10
votes
1 answer

Unable to use types between multiple Rust libraries via Python bindings created with PyO3

I am writing a Rust project with several libraries. Some of the libraries export types that are consumed by other libraries in the workspace. In addition to the Rust crates, I would also like to expose some of the libraries to Python, using the pyo3…
bnaecker
  • 6,152
  • 1
  • 20
  • 33
9
votes
0 answers

Error returning vector of document objects using pyo3

Returning Vector of document objects from rust to python fails. I have a struct and method implementation in rust as follows. use mongodb::{ bson::{Bson, Document}, error::Error, sync::Client, }; use pyo3::prelude::*; #[pyclass] pub…
8
votes
3 answers

Vector of custom struct in PyO3

I'm new to Rust and PyO3 (coming from Python) so this might be obvious to more experienced people. I declared a pyclass struct in PyO3. #[pyclass] struct Block { start: i32, stop: i32, } Then I use Block in a rust function that takes a…
kentwait
  • 1,969
  • 2
  • 21
  • 42
7
votes
2 answers

How do I make Cargo show what files are causing a rebuild?

I am using cargo, maturin and pytest to build a mixed Python/Rust project. During development, I frequently cycle through the commands: $ cargo test -p mypkg --release $ maturin develop --release $ python -m pytest --failed-first my_pkg It seemed…
Troy Daniels
  • 3,270
  • 2
  • 25
  • 57
7
votes
2 answers

Keyboard Interrupt from Python does not abort Rust function (PyO3)

I have a Python library written in Rust with PyO3, and it involves some expensive calculations (up to 10 minutes for a single function call). How can I abort the execution when calling from Python ? Ctrl+C seems to only be handled after the end of…
Neven V.
  • 424
  • 5
  • 13
7
votes
0 answers

Using PyAny to pass Rust-created objects from Python back to Rust

I have a struct + implementation in Rust that I return to Python. This object can also be passed back to Rust for further work. (In my actual code, I'm using a HashMap, but even just using a struct directly seems to cause the same…
user655321
  • 1,572
  • 2
  • 16
  • 33
6
votes
0 answers

How to exchange Polars-DataFrame between Rust and Python

I want to write a Python extension using Rust with Ctypes or Pyo3 to get better performance than native Python. But how to exchange data such as Polars DataFrame or ndarray type between Rust and Python?
Hakase
  • 211
  • 1
  • 12
6
votes
1 answer

How to structure a mixed Python Rust package with PyO3

I'm looking for Info on how to structure a Python package that wraps an extension module written in Rust, where both languages are mixed. I'm using pyO3 for FFI but can't seem to find an example on how to do this. To be specific: my rust library…
Adam
  • 743
  • 1
  • 6
  • 11
6
votes
1 answer

How to implement a Rust function that returns a Python object using PYO3

From Python, I want to call a Rust function that returns a Python object: my_rust_module.my_function() # => I am not sure how to create this function since the PYO3 guide on class instantiation describes how to instantiate such an object…
Ginty
  • 3,483
  • 20
  • 24
6
votes
2 answers

Writing a pyo3 function equivalent to a Python function that returns its input object

I am looking to write a Rust backend for my library, and I need to implement the equivalent of the following function in pyo3: def f(x): return x This should return the same object as the input, and the function getting the return value should…
Paul
  • 10,381
  • 13
  • 48
  • 86
5
votes
1 answer

Tell if object isinstance of PyDatetime or Pydate?

Here's my main.rs file: use pyo3::prelude::*; use pyo3::types::{PyDateTime, PyDate}; fn main() -> () { Python::with_gil(|py| { let datetime = py.import("datetime").unwrap(); let my_datetime = datetime.call_method1("datetime",…
ignoring_gravity
  • 6,677
  • 4
  • 32
  • 65
5
votes
1 answer

How to compose classes in pyo3?

I'm using the latest version of pyo3 (main branch) and it's not clear how I can store an instance of a class, say Store in the below example, on another class. For example, the following code composes two classes, Store and MyClass and will work to…
dspencer
  • 4,297
  • 4
  • 22
  • 43
5
votes
2 answers

How to call Rust async method from Python?

I want to use a Rust async method in Python. I'm trying to use PyO3 or rust-cpython. For example, for sync Rust functions, I can use, #[pyfunction] fn myfunc(a: String) -> PyResult { let mut contents = String::new(); contents =…
DumTux
  • 668
  • 1
  • 9
  • 24
1
2 3
10 11