This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author skoslowski
Recipients YannickJadoul, mattip, skoslowski
Date 2021-03-02.14:54:05
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1614696845.77.0.509207905.issue43367@roundup.psfhosted.org>
In-reply-to
Content
>>> 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
History
Date User Action Args
2021-03-02 14:54:05skoslowskisetrecipients: + skoslowski, mattip, YannickJadoul
2021-03-02 14:54:05skoslowskisetmessageid: <1614696845.77.0.509207905.issue43367@roundup.psfhosted.org>
2021-03-02 14:54:05skoslowskilinkissue43367 messages
2021-03-02 14:54:05skoslowskicreate