Questions tagged [pyo3]

Used for questions related to the Rust library pyo3.

pyo3 is a Rust library that provides bindings for Python.

161 questions
2
votes
1 answer

Garbage collection for vector of object in PyO3

I have 2 pyclasses Block and BlockGroup. #[pyclass] struct Block { start: i32, stop: i32, } #[pyclass] struct BlockGroup { blocks: Vec } I'm new to PyO3 and I have read the documentation about garbage collection but I don't…
kentwait
  • 1,969
  • 2
  • 21
  • 42
2
votes
1 answer

How to convert PyObjectRef to PyString?

I have a function that accepts a Python list. I expect the list to be made of strings. How do I extract them? fn f(list: &PyList) -> PyResult { for obj in list.iter() { let rust_string = PyString::from_object(obj, "ASCII",…
user7810882
  • 223
  • 1
  • 4
  • 14
1
vote
0 answers

Eclipse oddity trying to call Rust from Python using PyO3

I'm following the PyO3 Guide and have so far got to chapter 2, Python modules. OS is W10. I am entering all commands using the CLI outside Eclipse and just using the latter to edit files. NB the Rust add-on, Corrosion, provides some "intellisense"…
mike rodent
  • 14,126
  • 11
  • 103
  • 157
1
vote
1 answer

Rust to Python custom type covertion with pyo3

I'm trying to create a Python package with Pyo3 & Rust. in one of my functions, I'm trying to return a tch::Tensor type though not sure how to transform it so Python will be familiar with it. here's what I've tried so far: use pyo3::prelude::*; use…
gil
  • 2,388
  • 1
  • 21
  • 29
1
vote
1 answer

How to overwrite method in Python used in another method with mutable arguments in Rust?

I have two structs: Struct and InnerStruct. Struct has two methods: modify_object that uses modify_object_inner. Rust implementation of modify_object_inner doesn't matter because I want to implement this method in class that will inherit after my…
1
vote
0 answers

Can Python interact with structs owned by rust programs through Pyo3?

I have an api written in Rust but since a lot of colleagues only know Python I am trying to make it possible to build data pipelines in Python within the rust program. The idea would be make a Python interface to the rust code and vice versa. Then…
1
vote
1 answer

pyO3 and Panics

I have re-written some Python into Rust using pyO3. I have a situation where my Rust code can panic somewhere in a third party Rust library I use. The simplest thing for me would be to catch that panic in Python-land and fall back to the slower (but…
Stephen
  • 107
  • 9
1
vote
2 answers

PyO3 - How to return enums to python module?

I'm trying to build a Python package from Rust using PyO3. Right now I'm stuck trying to return enums Rust type to Python. I have a simple enum like so: pub enum Lang { Deu, Eng, Fra } And in lib.rs #[pyfunction] fn…
LeMoussel
  • 5,290
  • 12
  • 69
  • 122
1
vote
1 answer

How to add private methods to pyo3 pymethods?

I'd like to add hidden methods to pyo3 class methods implementation which will be invisible for Python. Example: #[pyclass] pub struct SomeItem; #[pymethods] impl SomeItem { #[new] pub fn new() -> Self { // visible for python constructor …
1
vote
1 answer

Return a Python object defined in a third party Python module (e.g. Numpy) using PyO3

Motivation This question is framed in terms of a specific example, for concreteness, but it's part of a much bigger question of how to return objects in pyo3, generally. Problem I'm writing a python module with a Rust backend using PyO3. I'd like to…
GHPR
  • 45
  • 6
1
vote
1 answer

With PyO3 how can I import a Python module once and then use it in multiple Rust threads?

Using PyO3, from a long-running Rust application I want to be able to call some functions many times. Before that I need to import a few modules. How can I do the imports just once and then re-use the PyModules from other threads that call the…
yolapo
  • 19
  • 1
1
vote
0 answers

Get Rust struct for parent or subclass from an attribute of a PyAny object

I have the following class structure defined in a Rust PyO3 Extension: #[pyclass(subclass)] struct Parent { foo: i32 } #[pyclass(extends=Parent)] struct Child { bar: i32 } Then in Python, a pure python class contains an attribute which can…
Darren
  • 1,774
  • 4
  • 21
  • 32
1
vote
1 answer

How to remove a lifetime requirement for an application in pyo3

Here's a reduced form of problem: I have a trait pub trait GetIter { type IntoIter: IntoIterator; fn get_iter( &self ) -> Self::IntoIter; } There are several use cases where I can implement GetIter on a reference &'a T, but not…
GHPR
  • 45
  • 6
1
vote
0 answers

How to use a python package in inline-python

A few days ago I came across by inline-python, which uses rust macro system to make developers able to write python code in rust, and uses PyO3 to do so, Im wondering is there a way to easily import packages of PyPi, without including their file in…
AARMN
  • 13
  • 3
1
vote
1 answer

Embedded a #[pyclass] in another #[pyclass]

I am trying to implement a cache for the private variable of any python class. Let's suppose I have this: #[pyclass] struct ClassA { pv: Py, // GIL independent type, storable in another #[pyclass] field_a:…
Jim
  • 450
  • 2
  • 10