title: Python conditional 'module object has no attribute' error with personal package distinct from circular import issue
body: <p>I'm getting a 'module object has no attribute ..." error when trying to use a package heirarchy I created.  The error is reminiscant of the error you get when there is a circular import (i.e. module a imports b and module b imports a), but I can't see that issue here.  I've gone through many posts with a similar error, but none of the explanations I saw quite fit.</p>
  
  <p>This was seen with python 2.7.1 and python 2.4.3.</p>
  
  <p>I've watered it down to the following example:</p>
  
  <p>Consider the following heirarchy (see code below):</p>
  
  <pre><code>alpha
  alpha/__init__.py
  alpha/bravo
  alpha/bravo/__init__.py
  alpha/bravo/charlie.py
  alpha/bravo/delta.py
  alpha/bravo/echo.py
  </code></pre>
  
  <p>The module charlie imports echo which in turn imports delta.  If the alpha/bravo/__init__.py (like alpha/__init__.py) is essentially blank, a script can do:</p>
  
  <pre><code>import alpha.bravo.charlie
  </code></pre>
  
  <p>The problem surfaces if I try to import alpha.bravo.charlie in alpha/bravo/__init__.py (with the thinking I could surface relevant classes/methods there, and a script would do 'import alpha.bravo').</p>
  
  <p>Code:</p>
  
  <p>alpha/__init__.py</p>
  
  <pre><code>(blank)
  </code></pre>
  
  <p>alpha/bravo/__init__.py</p>
  
  <pre><code>import alpha.bravo.charlie
  </code></pre>
  
  <p>alpha/bravo/charlie.py</p>
  
  <pre><code>import alpha.bravo.echo
  def charlie_foo(x): return str(x)
  def charlie_bar(x): return alpha.bravo.echo.echo_biz()
  </code></pre>
  
  <p>alpha/bravo/delta.py</p>
  
  <pre><code>def delta_foo(x): return str(x)
  </code></pre>
  
  <p>alpha/bravo/echo.py</p>
  
  <pre><code>import alpha.bravo.delta
  print alpha.bravo.delta.delta_foo(1)
  def echo_biz(): return 'blah'
  </code></pre>
  
  <p>If I try:</p>
  
  <pre><code>python -c 'import alpha.bravo'
  </code></pre>
  
  <p>I get:</p>
  
  <pre><code>Traceback (most recent call last):
    File "&lt;string&gt;", line 1, in &lt;module&gt;
    File "/home/kmkc980/svn/working/azcif/python/lib/alpha/bravo/__init__.py", line 1, in &lt;module&gt;
      import alpha.bravo.charlie
    File "/home/kmkc980/svn/working/azcif/python/lib/alpha/bravo/charlie.py", line 1, in &lt;module&gt;
      import alpha.bravo.echo
    File "/home/kmkc980/svn/working/azcif/python/lib/alpha/bravo/echo.py", line 2, in &lt;module&gt;
      print alpha.bravo.delta.delta_foo(1)
  AttributeError: 'module' object has no attribute 'bravo'
  </code></pre>
  
  <p>But, if I comment out the import line in alpha/bravo/__init__.py, then all seems OK:</p>
  
  <pre><code>python -c 'import alpha.bravo'
  
  python -c 'import alpha.bravo.charlie'
  1
  </code></pre>
  
  <p>Moreover, if I use the same code above (including the import line in alpha/bravo/__init__.py), but edit everything to exclude the 'alpha' level of the hierarchy, it seems to work fine.</p>
  
  <p>So the hierarchy is now just:</p>
  
  <pre><code>bravo
  bravo/__init__.py
  bravo/charlie.py
  bravo/delta.py
  bravo/echo.py
  </code></pre>
  
  <p>and I change all the lines with "alpha.bravo.*" to "bravo.*"</p>
  
  <p>Then no problem:</p>
  
  <pre><code>python -c 'import bravo'
  1
  </code></pre>
  
  <p>I've been able to work around the issue, but I'd still like to understand it. Thanks.</p>