New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
submodule of c-extension module is quirky #87533
Comments
If I have a module "parent", and I add another module "child" with a method "f" to it: child = PyModule_Create(...);
PyModule_AddObject(parent, "child", child); then I can call import parent
parent.child.f() but importing like this from parent.child import f raises a ModuleNotFoundError: ... 'parent' is not a package This came up in PyTorch pytorch/pytorch#38137 A complete example is attached If this is intentional, it might be nice to emit a warning when calling PyModule_AddObject with a module. |
first imports "parent" (successfully) but then fails, because the import code has no knowledge of were to find ".child". This is because If you instead run
only "parent" is imported, and "child" is retrieved as an attribute - which it actually is. The import code itself will add such an attribute, too [1]. However, that is after the submodule was located and loaded. Attribute lookup on the parent is not part of the submodule import itself. A (hacky) work-around would be to add an entry for "parent.child" in sys.modules. This prevents the import machinery from running. A (more) proper solution would be to mark "parent" as a package and install some importlib hooks. See [2] for an example from Cython-land. Finally there is PyImport_AppendInittab() [3], which could possibly be used to register "parent.child". I have never tried that. Even if this worked, it would be unsupported and probably not without side-effects. [1] https://docs.python.org/3/reference/import.html#submodules |
Bump Can somebody please explain the proper usage of the C API such that we can create actual packages and submodules from C code that are lazy loaded (Py_AppendInittab)? Edit: After some spelunking in the cpython code to find where the "is not a package" error was being thrown, it appears it was not happy with the fact that the parent package did not have the path dunder attribute set. As a test on the parent, I put a module exec slot on the parent and set the path attribute in the exec function to None. Then I created another module definition for a few sub modules of the parent package and appended them using the PyImport_AppendInittab function and dot notation. PyImport_AppendInittab(parent) The imports now work in a test python script -> import parent.sub.sub. No python, pure C code embedding python. Neat. Now I can lazy load with sub module support. Not sure if this is intended.. but unless somebody comes in here and says otherwise, I will run with this. I guess the recommendation here would be to set an exec slot on every module and set the path dunder to None. Additionally, set other attributes like @mattip's OP to make sure the python accesses work as expected. This can all be done lazily as a python script requests the modules instead of brute force creating all the modules up front. After all, lazy loading (PyImport_AppendInittab) was my goal here. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: