Ok, let's give you an example (original question below).
I have made a package called "myproject" which shows the problem I'm facing.
- Download the package here: https://dl.dropbox.com/u/11013311/myproject.zip
- Install the package (eg.: sudo python setup.py develop)
- open an Ipython session and type: "import myproject"
- if you start typing "myproject." and hit tab twice to get the available methods/values..., you'll see "myproject.myproject" which himself shows the same problem.
Is one of you guys able to explain me what I'm doing wrong? The answer from Andrew Alcock do not help me to fix it, or at least I don't see where could be the problem.
Of course, with such a simple example, there is no need for the package __init__.py
file to be that complex, but in my real project I do need COOL
instance to be accessible via myproject.COOL
.
Thanks for your answers!
EDIT: I've awarded utapyngo the bounty because his solution is effective and I've learnt something more (relative imports with deep submodules). But I'd like to thanks Andrew and nehz for their answers (nehz also provided a solution that fixed my problem, but very subjectively I find it 'less beautiful' ; and Andrew provided useful advices). Too bad I can't share the bounty.
**
ORIGINAL QUESTION:
**
I'm not sure I've phrased correctly the question. I have created a large code with a number of sub-packages, and for simplicity let's call it 'CODE'.
The problem is: 'CODE' appears in the namespace so I can have CODE.CODE
or CODE.CODE.CODE
, etc... an infinite number of times which looks strange to me and is probably (I guess) a hint that something is wrong (although the code works perfectly without warning).
I guess the problem has something to do with my __init__.py
and the structure of my code, so I give some more informations here.
Simplified code structure:
CODE
| __init__.py
| tools
| __init__.py
| mytools.py
| other
| __init__.py
| init.py
| sub
| __init__.py
| module.py
File: __init__.py
(the first one, at the root of CODE
)
import CODE.tools.mytools as MyTools
import CODE.other.init
OBJ = CODE.other.init.function()
...
The file mytools.py
do not import OBJ
from CODE, or any other module that may import OBJ
.
init.py
may import modules such as mytools.py
.
Finally, modules like module.py
may import either mytools.py
or OBJ
(from CODE). Usually, all import are made with absolute import, such as in: from CODE.sub.module import func
.
Does anyone have an explanation for such a behavior? I did not find any related question on SO, but it may be due to the wrong phrasing of mine.