Document how to call Python from Rust when the Python code has a nested directory structure
Please document how to call Python from Rust when the Python code has a nested directory structure.
I'd like to wrap a nested Python module (package?) using repeated calls to PyModule::from_code.
The code has a structure like this:
my_lib
├── a.py
├── b.py
├── foo
│ ├── c.py
│ ├── d.py
│ └── __init__.py
└── __init__.py
The goal is to be able to call into it using PyModule::from_code with code like this:
from my_lib import b
from my_lib.foo import c
def blarg():
return b.baz() + c.bagel()
Unfortunately, while the guide does describe how to call Python from Rust, and even how to handle dependencies, it doesn't describe how to handle this use-case.
In particular, I'm seeing errors like ModuleNotFoundError: No module named 'my_lib.foo'; 'my_lib' is not a package when adding c.py to my module structure.
I suspect this has something to do with the subtleties of modules, submodule, packages, and subpackages, but I haven't been able to figure it out - I could use some help from someone who understands this!
Thanks!
BTW, I'm in a situation like this and thus cannot simply install and then import my_lib.
Specifically, my code is in three layers: Rust, then Python, then Rust.
The bottom Rust layer defines types, the middle Python layer uses them to expose an API, and the top Rust layer uses the original Rust types with the Python API.
This whole thing needs to live inside a single binary, else I'll have duplicated and incompatible type objects (read through the above link for more info).
If you want to package a single binary, does pyoxidizer solve your use case?