[issue43367] submodule of c-extension module is quirky

Sebastian Koslowski report at bugs.python.org
Tue Mar 2 09:54:05 EST 2021


Sebastian Koslowski <sebastian.koslowski at gmail.com> added the comment:

>>> import parent.child

first imports "parent" (successfully) but then fails, because the import code has no knowledge of were to find ".child". This is because 
a) the module "parent" is not marked as a package (hence the error message) 
b) even if it were a package, there is no (ext) module file to locate and load.

If you instead run

>> from parent import child

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
[2] https://stackoverflow.com/questions/30157363/collapse-multiple-submodules-to-one-cython-extension
[3] https://docs.python.org/3/c-api/import.html?highlight=inittab#c.PyImport_AppendInittab

----------
nosy: +skoslowski

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue43367>
_______________________________________


More information about the Python-bugs-list mailing list