# Best Python bindings crate?

**URL:** <https://users.rust-lang.org/t/best-python-bindings-crate/64788>\
**Category:** uncategorized\
**Created:** [September 14, 2021, 4:11pm UTC](https://users.rust-lang.org/t/best-python-bindings-crate/64788 "2021-09-14T16:11:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![TimWescott](https://avatars.discourse-cdn.com/v4/letter/t/8c91f0/32.png) [@TimWescott](https://users.rust-lang.org/u/TimWescott)\
**Post date:** [September 14, 2021, 4:11pm UTC](https://users.rust-lang.org/t/best-python-bindings-crate/64788/1 "2021-09-14T16:11:41Z")

</div>

I'm working on a GUI-based application. It's written in Python and uses numpy for handing images around and doing processing on them.

I have some Rust code that uses ndarray, that I'd like to use for processing under the hood. I'm only interested in calling Rust code from Python -- not the other way around.

Typically, I expect to be calling Rust functions from Python code with arguments that may contain arrays, and getting responses that may themselves contain arrays. It would be nice if the translation from numpy array to ndarray and back were seamless.

Is there a "best" crate for this? Searches come up with pyO3 and cpython -- is there a clear winner between these for my purposes, or a third contender that I haven't encountered?

---

<div class="post-metadata">

**Author:** ![cole-miller](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/cole-miller/32/16722_2.png) [@cole-miller](https://users.rust-lang.org/u/cole-miller)\
**Post date:** [September 14, 2021, 10:38pm UTC](https://users.rust-lang.org/t/best-python-bindings-crate/64788/2 "2021-09-14T22:38:00Z")

</div>

As far as I can tell from posts on this forum, PyO3 is much more widely used than cpython. The stats at lib.rs bear this out [[1](https://lib.rs/crates/cpython), [2](https://lib.rs/crates/pyo3)]. Both libraries are maintained, but PyO3 has had more frequent releases than cpython recently; the last release of cpython was 5 months ago.

You might be able to use the [numpy](https://github.com/PyO3/rust-numpy) crate—part of the PyO3 ecosystem—to hook up numpy to ndarray, but I'm not sure about that. It seems to be focused on the "calling Python from Rust" use-case.

[Here](https://pyo3.rs/v0.14.5/rust_cpython.html) is a page from the PyO3 documentation that contrasts it with cpython.

---

<div class="post-metadata">

**Author:** ![Michael-F-Bryan](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/michael-f-bryan/32/28749_2.png) [@Michael-F-Bryan](https://users.rust-lang.org/u/Michael-F-Bryan)\
**Post date:** [September 15, 2021, 4:36am UTC](https://users.rust-lang.org/t/best-python-bindings-crate/64788/3 "2021-09-15T04:36:44Z")

</div>

I've used `pyo3` in the past and it's done pretty well for what I need.

I wouldn't worry too much about finding the "best" crate. Just find something that works (both `pyo3` and `cpython` as re quite capable) then structure your code so the Python-Rust interface is as thin and simple as possible (e.g. translate arguments then call a function from the main Rust crate) and you should be fine.

---

<div class="post-metadata">

**Author:** ![TimWescott](https://avatars.discourse-cdn.com/v4/letter/t/8c91f0/32.png) [@TimWescott](https://users.rust-lang.org/u/TimWescott)\
**Post date:** [September 15, 2021, 3:50pm UTC](https://users.rust-lang.org/t/best-python-bindings-crate/64788/4 "2021-09-15T15:50:05Z")

</div>

I'm definitely really only interested in the "call Rust from Python" use case, unless for some reason I need callbacks.

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [December 14, 2021, 3:50pm UTC](https://users.rust-lang.org/t/best-python-bindings-crate/64788/5 "2021-12-14T15:50:28Z")

</div>

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.
