Importing modules used in a package in __init__.py
<p>I have the following directory structure:</p>

<pre><code>funniest/
    setup.py
    funniest/ 
          __init__.py
          ModuleA.py
          ModuleB.py
</code></pre>



<pre><code># __init__.py
from numba import jit

from .ModuleA import ClassA
</code></pre>



<pre><code># ModuleA.py
import funniest.ModuleB

class ClassA():
    pass
</code></pre>



<pre><code># ModuleB.py
@jit
def f():
    pass
</code></pre>

<p>However, when importing the module, I get the error message:</p>

<pre><code>&gt;&gt;&gt; import funniest
Traceback (most recent call last):
File "&lt;stdin&gt;", line 1, in &lt;module&gt;
File "/home/domi89/Dropbox/Python_Projects/funniest/funniest  /__init__.py", line 2, in &lt;module&gt;
from .ModuleA import ClassA
File "/home/domi89/Dropbox/Python_Projects/funniest/funniest /ModuleA.py", line 1, in &lt;module&gt;
import funniest.ModuleB
File "/home/domi89/Dropbox/Python_Projects/funniest/funniest/ModuleB.py", line 1, in &lt;module&gt;
@jit
NameError: name 'jit' is not defined
</code></pre>

<p>I cannot understand why this happends, because when <code>__init__.py</code> get executed first, the first thing it does is importing <code>from numba import jit</code>. So why is <code>jit</code> not defined when it comes to function <code>f</code> in <code>ModuleB</code>?    </p>

<p>The name <code>jit</code> is not known inside <code>ModuleB</code>. Python imports doesn't work like C/C++ includes that roll-out the code in place of the <code>import</code>. A new module creates a new namespace with a map inside - a map with functions, classes, etc.</p>

<p>So when you did <code>import funniest</code>, you created new map entry in the current namespace, like: <code>{..., funniest: {}, ...}</code>.  By importing <code>funniest</code>, you imported <code>jit</code> and <code>ClassA</code>, so from the place when you imported the <code>funniest</code> module, you have access to such map: <code>funniest: {jit: {..}, ClassA: {...}}</code>.</p>

<p>In <code>ModuleA</code> you imported <code>ModuleB</code>, so in <code>ModuleA</code> you have new variable <code>ModuleB:{...}</code> with content of <code>ModuleB</code>. But here you don't see the <code>funniest</code> map - it is in other level in hierarchy.</p>

<p>And in <code>ModuleB</code>, you don't see <code>funniest</code> and <code>ClassA</code> maps (namespaces - and whatever you like to call it;)).</p>

python