Is it bad practice to programmatically add submodules to sys?

22 hours ago 2
ARTICLE AD BOX

I made a small Python module that has a custom _all_ and want to group certain objects into submodules. More specifically, I want the primary functionality of my code accessible through * imports and also want the types I use internally to be accessible in bulk but not necessarily with the rest of the module. So something like this :

from mymodule import * # Primary from mymodule.types import * # Extra

The first thing that came to mind was grouping with different files, but since it's such a small module that feels excessive. When researching it, I learned it was possible to do it all in one file by instantiating a module object using types.ModuleType and forcing that directly into sys.modules. But to me this seems quite messy and I'm honestly surprised it's even possible. Is this always bad practice, or are there situations where it's acceptable?

Read Entire Article