Q on naming nested packages/modules
no.email at please.post
Tue Sep 1 17:58:00 CEST 2009
I'm having a hard time getting the hang of Python's package/module
scheme. I'd like to find out what's considered best practice when
dealing with the scenario illustrated below.
The quick description of the problem is: how can I have two nested
modules, spam.ham and spam.ham.eggs?
Suppose I have a module (I'm not even sure this is the right word)
called spam.ham, so I start out with the following file structure:
With this arrangement, the line
...in client code works as expected.
But now suppose that I want to factor out some code in spam/ham.py
to a helper module. (The reason behind factoring out this new
module is to "declutter" spam/ham.py, and improve its readibility.)
My instinct (from my Perl past) is to put this factored-out code
in a file spam/ham/eggs.py, i.e. to create the "nested" module
spam.ham.eggs, which requires expanding the tree as follows
| |-- eggs.py
| `-- __init__.py
...and adding the following spam/ham.py to
from . import eggs
This doesn't work so well, because now spam/ham.py is not read.
It seems that adding the spam/ham directory, or maybe adding the
file spam/ham/__init__.py, causes spam/ham.py to be overlooked.
Clearly, I'm not playing this game right...
What is considered "best practice" for the use case sketched above?
Should I, e.g. rename the directory spam/ham something like spam/ham_
and refer to the helper module as spam.ham_.eggs? Or is some other
I consulted PEP 8, but besides recommending "short, all-lowercase
names" for modules, it gives little guidance on the situation
More information about the Python-list