[Python] Interoperability between two crates with `pyo3` bindings

Discussão em 'Python' iniciado por Stack, Abril 9, 2025.

  1. Stack

    Stack Membro Participativo

    I have two Rust crates lib1 and lib2, for which I use pyo3 to generate bindings. I import the crate lib1 in lib2.

    In a separate crape lib1-py, I create a python class MyClass, and in lib2, I try to use MyClass as a parameter for a Python function, using :

    fn foo(param: PyRef<MyClass>)


    I separately use maturin to build both lib1-py and lib2 in the same environment.

    When I try to run the python code:

    import lib1
    import lib2

    param = lib1.MyClass()
    lib2.foo(param)


    I obtain the error TypeError: argument 'param': 'MyClass' object cannot be converted to ‘MyClass’.

    I suppose that Rust's crate system tries to be safe by treating types from different crates as separate entities, even if they are identical. How could I solve such an issue?

    Continue reading...

Compartilhe esta Página