need clarification with import statements

John Machin sjmachin at lexicon.net
Thu Dec 14 21:39:51 EST 2006


Tool69 wrote:
> Hi,
> I've got the following hierarchy:
>
> mainprog/
>     __init__.py
>     prog.py
>     utils/
>         __init__.py
>         myutils.py
>     others/
>         __init__.py
>         myothers.py
>
> Inside prog.py I need to have full access to myutils.py and
> myothers.py;
> Inside myutils.py, I need to access two classes from myothers.py (ie
> myotherClass1 and myotherClass2);
> Inside myothers.py, I need some functions of myutils.py (ie
> myutils_func1 and myutils_func2);
>
> Do you have some hints please ?

1. Your directory/package hierarchy is far too complicated. Flatten it.

2. You have circular references: the others module will import from
utils, but utils wants to import from others. This is prima facie
evidence that your modules are not structured properly. Rule 1: Modules
should contain /related/ classes and functions. Rule 2: A graph of what
imports what should not have loops.

Consider combining others and utils -- does that make sense?

Another alternative: split out those "some functions of myutils.py (ie
myutils_func1 and myutils_func2)" into a fourth module. This module
might be the nucleus of a tool-kit of miscellaneous functions that you
might import in /any/ app -- in that case, move it outside the current
package.

HTH,
John




More information about the Python-list mailing list