need clarification with import statements

T

Tool69

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 ? I'm trying to avoid name conflicts, so
I haven't used "from ... import *".
I suspect ther's something wrong, but I think I really don't understand
what's going on internally (no errors (for the moment !))

Inside prog.py : from utils.myutils import myotherClass1, myotherClass2
+ some functions of myutils I need
Inside myutils.py : from other.myothers import myotherClass1,
myotherClass2
Inside others.py : import utils.myutils

Thanks.
 
J

John Machin

Tool69 said:
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
 
T

Tool69

Hi John,
1. Your directory/package hierarchy is far too complicated. Flatten it.
Ok.

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

Yes, that's why I asked the question in fact, circular references are a
bit hazardous.
. 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?

Yes, I can combine them as they need eachother.
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.

Good idea indeed,
Thanks for all,
6TooL9
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top