title: Resolve circular import error in python
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/744373/circular-or-cyclic-imports-in-python">Circular (or cyclic) imports in Python</a>  </p>
  </blockquote>
  
  
  
  <p>I have class B that imports and creates instances of class A.
  Class A needs reference to B in its contructor and so includes B.</p>
  
  <pre><code>from a import A
  class B:
    def __init__(self):
      self.a = A()
  </code></pre>
  
  <hr>
  
  <pre><code>from b import B
  class A:
    def __init__(self, ref):
      assert isinstance(ref, B)
      self.ref = ref
  </code></pre>
  
  <p>This doesn't work. The main file imports B and uses it... not.
  Something with the imports is wrong.</p>
  
  <p>Error from file a
  <strong>ImportError: cannot import name B</strong></p>