PyO3 submodules in Rust/Python mixed projects #1540
-
Hi there, I have a Rust project that defines a few Python submodules. I can import these without a problem with What am I missing ? Thanks for your help.
Example from // (...)
pub(crate) fn register_cosmic(py: Python<'_>, parent_module: &PyModule) -> PyResult<()> {
let sm = PyModule::new(py, "nyx_space.cosmic")?;
sm.add_class::<Cosm>()?;
sm.add_class::<Bodies>()?;
sm.add_class::<Frame>()?;
sm.add_class::<Orbit>()?;
sm.add_class::<Spacecraft>()?;
sm.add_class::<SrpConfig>()?;
sm.add_class::<DragConfig>()?;
sm.add_class::<Thruster>()?;
sm.add_class::<GuidanceMode>()?;
py_run!(py, sm, "import sys; sys.modules['nyx_space.cosmic'] = sm");
parent_module.add_submodule(sm)?;
Ok(())
}
// (...) I'm trying to add some Python only functions to the package. Here is my
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 8 replies
-
I think you want to use |
Beta Was this translation helpful? Give feedback.
-
Summary of the solution:
Now, you can expand the
|
Beta Was this translation helpful? Give feedback.
Summary of the solution:
0.14.16
or higherregister_
functions that add the submodule, make sure to use the package name without the underscore, e.g.py_run!(py, sm, "import sys; sys.modules['nyx_space.cosmic'] = sm");
Now, you can expand the
__init__.py
file to include additional attributes like__all__
: