title: What can I do about "ImportError: Cannot import name X" or "AttributeError: ... (most likely due to a circular import)"?
body: <p>I have some code spread across multiple files that try to <code>import</code> from each other, as follows:</p>
  <p>main.py:</p>
  <pre><code>from entity import Ent
  </code></pre>
  <p>entity.py:</p>
  <pre><code>from physics import Physics
  class Ent:
      ...
  </code></pre>
  <p>physics.py:</p>
  <pre><code>from entity import Ent
  class Physics:
      ...
  </code></pre>
  <p>I then run from <code>main.py</code> and I get the following error:</p>
  <pre><code>Traceback (most recent call last):
  File &quot;main.py&quot;, line 2, in &lt;module&gt;
      from entity import Ent
  File &quot;.../entity.py&quot;, line 5, in &lt;module&gt;
      from physics import Physics
  File &quot;.../physics.py&quot;, line 2, in &lt;module&gt;
      from entity import Ent
  ImportError: cannot import name Ent
  </code></pre>
  <p>I'm assume the error is due to importing <code>entity</code> twice - once in <code>main.py</code> and later in <code>physics.py</code> - but how can I work around the problem?</p>
  <hr />
  <p><sub>See also <a href="https://stackoverflow.com/questions/744373">What happens when using mutual or circular (cyclic) imports in Python?</a> for a general overview of what is allowed and what causes a problem WRT circular imports. See <a href="https://stackoverflow.com/questions/22187279">Why do circular imports seemingly work further up in the call stack but then raise an ImportError further down?</a> for technical details on <strong>why and how</strong> the problem occurs.</sub></p>