Note that there are some explanatory texts on larger screens.

plurals
  1. POCircular intra-referencing submodules
    text
    copied!<h1>Words</h1> <p>The structure is as follows: a module <code>test</code> contains two submodules <code>test.foo</code> and <code>test.bar</code>.</p> <p><code>test.foo</code> has a function <code>inc()</code> that uses <code>test.bar.bar()</code> so based on the python documentation <code>from . import bar</code> is the proper way to include that, and this works as expected.</p> <p><code>test.bar</code> however, also has a function <code>inc2</code> that uses <code>test.foo.foo</code>, but when <code>from . import foo</code> is used, both of these modules break.</p> <p>What is the correct method for achieving this? I've found little in the python docs or searching.</p> <h1>Code</h1> <p><strong>test/_<em>init</em>_.py</strong></p> <pre><code>#empty </code></pre> <p><strong>test/foo.py</strong></p> <pre><code>from . import bar def foo(): print("I do foo") def inc(): print(bar.bar()) </code></pre> <p><strong>test/bar.py</strong></p> <pre><code>from . import foo def bar(): print("I do bar") def inc2(): print(foo.foo()) </code></pre> <p><strong>Error 1</strong></p> <pre><code>&gt;&gt;&gt; import test.foo Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; File "test/foo.py", line 1, in &lt;module&gt; from . import bar File "test/bar.py", line 1, in &lt;module&gt; from . import foo ImportError: cannot import name foo </code></pre> <p><strong>Error 2</strong></p> <pre><code>&gt;&gt;&gt; import test.bar Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; File "test/bar.py", line 1, in &lt;module&gt; from . import foo File "test/foo.py", line 1, in &lt;module&gt; from . import bar ImportError: cannot import name bar </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload