'Python module implemented like a package
I have an application containing a package, thus:
* fruits/
| citrus/
| | __init__.py
| | oranges.py
| | mandarins.py
| | satsumas.py
| | ... etc
The Python files under fruits/citrus/
contain definitions of about 300 subclasses of fruits.citrus.Citrus
.
They live in separate files like this only for administrative reasons. Nobody should know or care about fruits.citrus.mandarins
etc unless they're working on the fruits.citrus
package. To put it another way, I would like this package to pretend it's a module.
I have tried putting this in __init__.py
:
from fruits.citrus.oranges import *
from fruits.citrus.mandarins import *
from fruits.citrus.satsumas import *
[...]
g = list(globals().items())
__all__ = list([
name for name, value in g
if value.__class__==type and
issubclass(value, Citrus)
])
This sort of works, in that you can see fruits.citrus.Tangerine
. But pydoc and sphinx still list oranges
, mandarins
, etc as package contents. And Tangerine
is both at fruits.citrus.Tangerine
and at fruits.citrus.mandarins.Tangerine
, so things are even more complicated than when I started.
There has to be a way to do this, but everything I tried has been a lemon. Help?
Solution 1:[1]
Turns out there isn't a good way, but that's all right.
I was trying to use an autodoc program to document a codebase, and it kept listing all these classes twice. The answer in the end was just to specify the classes by hand!
Solution 2:[2]
can you tell if that helps hiding those submodules:
from .mandarins import *
from .oranges import *
del globals()['mandarins']
del globals()['oranges']
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Marnanel Thurman |
Solution 2 | Marcin Cuprjak |