Questions tagged [pyo3]

Used for questions related to the Rust library pyo3.

pyo3 is a Rust library that provides bindings for Python.

161 questions
1
vote
1 answer

PyO3 convert rust struct to PyObject

I have a simple class annotated with #[pyclass] #[pyclass] pub struct A { ... } Now I have a function of the form fn f(slf: Py) -> PyObject{ //... some code here let output = A{...}; output.to_object() // Error: method…
alagris
  • 1,838
  • 16
  • 31
1
vote
0 answers

Unable to use pip modules with PyO3

Recently I have been working on a project that involves generating docx files. Since Rust's docx support is still quite immature, I've decided to use Python's python-docx module via PyO3. Here's my code so far: extern crate pyo3; use…
RiverSong
  • 21
  • 3
1
vote
1 answer

How to call Python async function from Rust?

I have read this answer(How to call Rust async method from Python?) and what I want to do is the opposite of this. I want to call a Python async function and await it in tokio's runtime. Is it possible? I have given it some tries but I am facing an…
Sanskar Jethi
  • 544
  • 5
  • 17
1
vote
1 answer

How to pass an async function as a parameter in Rust PyO3

When we write vanilla rust and we have to pass in an async function as an argument to another function we do the following: pub f( test: &dyn Fn(&'a mut String, String, String, TcpStream) -> F, ) where F: Future + 'a, But…
Sanskar Jethi
  • 544
  • 5
  • 17
1
vote
1 answer

How do I generate a macro from previous macro-generated code?

In order to create a pyo3-powered Python class working with a struct that uses generic type, I want to use wrappers that will generate the code needed to not have to do it for every specific type. I created a macro that generates the code but I need…
litchipi
  • 53
  • 1
  • 5
1
vote
1 answer

Pyo3: The trait `PyClass` is not implemented for `&Py`

I am learning rust and trying to make a very simple python module using pyo3 and maturin. I am having a problem on the rust code though, Cargo.toml [package] name = "lenrs" version = "0.1.0" authors = ["matheusfillipe"] edition =…
mattf
  • 85
  • 1
  • 8
1
vote
2 answers

PyO3 can't find Python interpreter when using Anaconda

Consider the following Rust code: use pyo3::prelude::*; fn main() -> PyResult<()>{ let gil = Python::acquire_gil(); let py = gil.python(); let result = py.run("print('it works')", None, None); if let Err(ref err) = result { …
Péter Leéh
  • 2,069
  • 2
  • 10
  • 23
1
vote
0 answers

Rust pyo3: Any way to pass argument to pyfunction by reference?

fn gen_data<'py> (py: Python<'py>, a: Vec, b: Vec) -> PyResult<&'py PyBytes>{ let dp = MyDataStruct { data: Bytes::from(a), extra_info: Bytes::from(b), }; Ok(PyBytes::new(py,…
Li haonan
  • 600
  • 1
  • 6
  • 24
1
vote
0 answers

How to make a Python "rust-Extension" module that behaves exactly like C-Extensions in terms of call overhead and processing speed?

The closer option I have found is pyo3, but it isn't clear to me if it adds any extra overhead when compared to the traditional C-extensions. From here it seems like such C-extension behavior is possible through borrowed objects (I still have to…
dawid
  • 663
  • 6
  • 12
1
vote
0 answers

rust linking fails with aarch64-linux-android-clang, cannot find -lpython3.6m

So, to give a bit of context: I want to use a foreign (rust) library with python Kivy application for experimental self-educational purposes. I wasn't sure where to start since I never was too deep in compilation process so I decided to try using…
Nikolai Savulkin
  • 697
  • 5
  • 12
1
vote
1 answer

How can I convert PyArray into Vec> in Rust

I am using the numpy crate in Rust to work with 2D arrays that come from python. PyArray (https://docs.rs/numpy/0.11.0/numpy/array/struct.PyArray.html) implements a from_vec2() function, which converts a Vec> into a PyArray (2D…
Yaxlat
  • 665
  • 1
  • 10
  • 25
1
vote
0 answers

Pyo3: Why isn't an error thrown when using an incorrect constructor?

I am writing a python library with a Rust backend using pyo3 and I don't understand why there isn't an error thrown when you call the wrong constructor, I think what I'm saying will make more sense when I show the code. // vector.rs use…
1
vote
1 answer

PyO3 built rust binding executable gives import error?

I recently came across PyO3 and I have successfully built the example code from PyO3 repo. Cargo.toml [package] name = "string-sum" version = "0.1.0" edition = "2018" [lib] name = "string_sum" crate-type = ["cdylib"] [dependencies.pyo3] version =…
Eka
  • 14,170
  • 38
  • 128
  • 212
1
vote
1 answer

How do I unmarshal a PyCodeObject using PyO3?

I am reading .pyc files, and need to be able to unmarshal code objects. When I try to downcast the unmarshalled PyAny to PyCodeObject, I get the following error message: error[E0277]: the trait bound `pyo3::ffi::code::PyCodeObject:…
Solomon Ucko
  • 5,724
  • 3
  • 24
  • 45
1
vote
1 answer

Pycharm doesn't recognize compiled .pyd even though it works when the script runs

I have a binary module made with pyo3 and rust. It is in a directory with a script that imports it. The directory is marked as source root, so that shouldn't be the problem. There are red squiggly marks under the import, but when I run the script,…