Best pattern for making calls from Rust to a large Python package? #2402
-
I have a relatively large From what I've read so far, it seems there might be two strategies:
I would prefer not to have to write the multiple compilation logic myself, but this makes me think I might have to. Is there I'm missing? How should I do this? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
What's the entry point of your program? Do you want to ship a single executable, or are you fine to create a Python environment? If you want a single executable, PyOxidizer has worked pretty well for many users. If you are fine to create a Python environment, then I'd compile your Rust as a Python extension and use it as part of normal Python scripting. Usually the best way to call into Python would be to import it via normal mechanisms: #[pyfunction]
fn call_some_foo(py: Python<'_>) -> PyResult<()> {
let foo = PyModule::import(py, "foo")?;
// call a function in the foo package
foo.getattr("some_function")?.call0()?;
Ok(())
} |
Beta Was this translation helpful? Give feedback.
What's the entry point of your program? Do you want to ship a single executable, or are you fine to create a Python environment?
If you want a single executable, PyOxidizer has worked pretty well for many users.
If you are fine to create a Python environment, then I'd compile your Rust as a Python extension and use it as part of normal Python scripting.
Usually the best way to call into Python would be to import it via normal mechanisms: