python absolute import of submodule fails
<p>I have seen other posts but did not find answer that worked!</p>

<p>File structure</p>

<pre><code>my_package/
      __init__.py -- empty
      test/
              __init__.py -- empty
              test1.py 
</code></pre>

<p>Fail</p>

<pre><code>from my_package import test
test.test1
</code></pre>

<p>gives </p>

<pre><code>AttributeError: 'module' object has no attribute test
</code></pre>

<p>Following Passes</p>

<pre><code>from my_package.test import test1

# or
import my_package.test.test1
from my_package import test
# now this works
test.tes1

&lt;module 'my_package.test.test1' from ...
</code></pre>

<p>I have </p>

<pre><code> from __future__ import absolute_import 
</code></pre>

<p>in all files, and using python2.7</p>

<p>When you import a package (like <code>test</code>), the modules (like <code>test1</code>) are not automatically imported (unless perhaps you put some special code to do so in <code>__init__.py</code>). This is different from importing a module, where the contents of the module are available in the modules namespace. Compare with the Python standard library's <code>xml.etree.ElementTree</code> module:</p>

<pre><code>&gt;&gt;&gt; import xml
&gt;&gt;&gt; xml.etree
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
AttributeError: 'module' object has no attribute 'etree'
&gt;&gt;&gt; from xml import etree
&gt;&gt;&gt; etree.ElementTree
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
AttributeError: 'module' object has no attribute 'ElementTree'
&gt;&gt;&gt; from xml.etree import ElementTree
&gt;&gt;&gt; ElementTree.ElementTree
&lt;class 'xml.etree.ElementTree.ElementTree'&gt;
</code></pre>

python python-2.7 import package python-import