Python Imports failing. Relative imports, package recognition, __init__.py , __package__, __all__
<p>I've been reading a lot of threads, the PEP articles related to this problem, which are around 4 of them, but none of them gives a clear idea on some points and I still can't do relative imports. </p>

<p>In fact, the contents of my main package is not listed at all.</p>

<p><strong>REEDITION. I modified all the post, it was too complex and questions were a lot.</strong></p>

<p>In <code>C:/test/</code> I have this package:</p>

<pre><code>Package/ (FOLDER 2)
    __init__.py
    moduleA.py
    moduleB.py
</code></pre>

<ul>
<li><code>moduleA</code> imports <code>moduleB</code>, and viceversa.</li>
<li><code>__init__.py</code> is empty</li>
</ul>

<p>My process:</p>

<ol>
<li>I add <code>C:/test/</code> to <code>sys.path</code>.</li>
<li><code>import Package</code>  (WORKS) </li>
<li><code>dir(Package)</code>  <strong>Doesn't list any module inside Package.</strong></li>
<li>Package is: <code>&lt;module âPackageâ from C:/test/Package/_init_.py&gt;</code></li>
<li><code>__file__</code> is the init file under Package</li>
<li><code>__name__</code> is <code>Package</code></li>
<li><code>__package__</code> <strong>is an empty string</strong></li>
<li><code>__path__</code> is <code>C:/test/Package</code></li>
</ol>

<hr>

<p>TEST 1 - Version 1:
in <code>moduleA</code> I have <code>from Package import moduleB</code></p>

<p>I get this:</p>

<pre><code>&gt;&gt;&gt; import Package.moduleA
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  File "C:/test\Package\moduleA.py", line
    from Package import moduleB
  File "C:/test\Package\moduleB.py", line
    from Package import moduleA
ImportError: cannot import name moduleA
</code></pre>

<p><em>It doesn't work because <code>moduleA</code> is not part of <code>Package</code>. So <code>Package</code> is not recognized as a package?</em></p>

<hr>

<p>TEST 1 - Version 2:
in <code>moduleA</code> I have <code>from . import moduleB</code></p>

<p>Doesn't work, same error</p>

<hr>

<p>TEST 1 - Version 3: 
in <code>moduleA</code> I have <code>import Package.moduleB</code></p>

<p>It works.</p>

<p>After that, running:</p>

<pre><code>&gt;&gt;&gt; dir(Package.moduleB)
['Package', '__builtins__', '__doc__', '__file__', '__name__', '__package__']
&gt;&gt;&gt; Package.moduleB.Package
&lt;module 'Package' from 'C:/prueba\Package\__init__.py'&gt;
&gt;&gt;&gt; dir(Package)
['__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', 'moduleA', 'moduleB']
</code></pre>

<p>So now, <code>Package.moduleB</code>  has  <code>Package</code> as a variable 
And surprisingly, <code>Package</code> how looks like a proper package, and contains both modules.</p>

<p>In fact, doing any of imports in Version 1 and Version 2 work now, because now <code>moduleA</code> and <code>moduleB</code> are part of <code>Package</code>.</p>

<hr>

<p>Questions:</p>

<p>1) Why is <code>Package</code> not recognized as a package? Is it? Shouldn't it contain all submodules? </p>

<p>2) Why running <code>import Package.moduleA</code>  generates <code>Package</code> inside <code>moduleA</code>?</p>

<p>3) Why running <code>import Package.moduleA</code> adds <code>moduleA</code> to <code>Package</code> and it wasn't there before?</p>

<p>4) Does an empty <code>__init__.py</code> file, and a NON empty <code>__init__.py</code> file affect to this at all? </p>

<p>5) Does defining an <code>__all__</code> variable containing <code>['moduleA', 'moduleB']</code> would do anything here?</p>

<p>6) How can I make the init file to load both submodules? Should I do <code>import Package.moduleA</code> and <code>Package.moduleB</code> inside?... can't I do it relatively like <code>import .moduleA as moduleA</code> or something like that? (What If the name of <code>Package</code> changes?)</p>

<p>7) Does that empty string on the package variable affect? We should change it's contents if we want it to recognize itself... <code>__package__</code> should be the same as <code>__name__</code>, or that's what the PEPs say. But doing this didn't work:</p>

<pre><code>if __name__ == "__main__" and __package__ is None:
    __package__ = "Package"
</code></pre>

<p>This is a Python bug that exists in Python versions prior to 3.5. See <a href="http://bugs.python.org/issue992389" rel="nofollow">issue 992389</a> where it was discussed (for many years) and <a href="http://bugs.python.org/issue17636" rel="nofollow">issue 17636</a> where the common cases of the issue were fixed.</p>

<p>After the fix in Python 3.5, an explicit relative import like <code>from . import moduleA</code> from within a module within the package <code>Package</code> will check in <code>sys.modules</code> for <code>Package.moduleA</code> before giving up if <code>moduleA</code> isn't present yet in <code>Package</code>. Since the module object is added to <code>sys.modules</code> before it starts loading, but is not added to <code>Package.__dict__</code> until after the loading is complete, this usually fixes the problem.</p>

<p>There can still be a problem with circular imports using <code>from package import *</code>, but in <a href="http://bugs.python.org/issue23447" rel="nofollow">issue 23447</a> (which I contributed a patch for), it was decided that fixing that even more obscure corner case was not worth the extra code complexity.</p>

<p>Circular imports are usually a sign of bad design. You should probably either refactor the bits of the code that depend on each other out into a single utility module that both of the other modules import, or you should combine both of the two separate modules into a single one.</p>

python import python-2.x