title: Python circular import, `from lib import module` vs `import lib.module`
body: <p>I have two python modules, <code>a.py</code> and <code>b.py</code>, both of which are in <code>lib/</code> relative to the current directory. Suppose each module needs the functionality of the other.</p>
  
  <p><strong>a.py:</strong></p>
  
  <pre><code>import lib.b
  ...
  </code></pre>
  
  <p><strong>b.py:</strong></p>
  
  <pre><code>import lib.a
  ...
  </code></pre>
  
  <p>The above example works with</p>
  
  <pre><code>PYTHONPATH=./lib python -c 'from lib import a, b'
  </code></pre>
  
  <p>However, if I switch the imports in <code>a.py</code> and <code>b.py</code> to <code>from lib import b</code> and <code>from lib import a</code>, respectively, the above Python command terminates with <code>ImportError</code>.</p>
  
  <p>Could someone please explain why this breaks? I'm not trying to import any member from either <code>a</code> or <code>b</code>. (In that case I would be importing from an uninitialized module, as the question referenced below points out.)</p>
  
  <h3>Reference:</h3>
  
  <ol>
  <li><a href="https://stackoverflow.com/questions/3955790/python-circular-imports-once-again-aka-whats-wrong-with-this-design">python circular imports once again (aka what&#39;s wrong with this design)</a></li>
  </ol>