Questions tagged [pyo3]

Used for questions related to the Rust library pyo3.

pyo3 is a Rust library that provides bindings for Python.

60 questions
13
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
  • 131
  • 2
7
votes
0 answers

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
  • 5,114
  • 1
  • 11
  • 29
7
votes
0 answers

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
  • 171
  • 1
  • 1
6
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.
  • 414
  • 5
  • 11
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
  • 9,014
  • 9
  • 41
  • 75
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 =…
Hotte Shen
  • 615
  • 4
  • 19
5
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,643
  • 2
  • 19
  • 38
4
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…
4
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,148
  • 2
  • 11
  • 22
4
votes
2 answers

How can I implement python operators in PyO3

I'm trying to implement a vector class in rust for my math library. #[pyclass] struct Vec2d { #[pyo3(get, set)] x: f64, #[pyo3(get, set)] y: f64 } But I can't figure out how I can overload the standard operators (+, -, *, /) I…
N0name
  • 133
  • 7
4
votes
1 answer

How to implement trait FromPyObject for my rust struct

Consider a simple rust class exposed via pyo3 to python use pyo3::prelude::*; #[pyclass(name=MyClass)] pub struct PyMyClass { // Some fields } #[pymethods] impl PyMyStruct { #[new] fn py_new(obj: PyRawObject) { obj.init({ …
Matthias
  • 795
  • 1
  • 8
  • 19
3
votes
2 answers

How to create a heterogeneous Python dict in Rust with PyO3?

Based on this I can create a homogeneous Python dictionary. How can I create a dictionary with mixed-type values, e.g. make this work: let dict = [ ("num", 8), ("str", "asd") ].into_py_dict(py); ?
tungli
  • 313
  • 1
  • 6
3
votes
2 answers

Raising an exception with pyo3

How do I correctly raise an Exception? I've tried the following: #[pymethods] impl Foo { #[new] fn __new__(arg1: u8, ...) -> Self { if arg1 > LIMIT { let gil = Python::acquire_gil(); let py = gil.python(); …
Martin Bammer
  • 351
  • 3
  • 15
3
votes
1 answer

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

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,399
  • 18
  • 21
2
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
  • 3,649
  • 4
  • 15
  • 35
1
2 3 4