title: Imports in __init__.py and 'import as' statement
body: <p>I've run into a problem with having imports in <code>__init__.py</code> and using <code>import as</code> with absolute imports in modules of the package.</p>
  <p>My project has a subpackage and in its <code>__init__.py</code> I &quot;lift&quot; one of the classes from a module to the subpackage level with <code>from import as</code> statement. The module imports other modules from that subpackage with absolute imports. I get this error <code>AttributeError: 'module' object has no attribute 'subpkg'</code>.</p>
  <h1>Example</h1>
  <p><strong>Structure</strong>:</p>
  <pre><code>pkg/
  ├── __init__.py
  ├── subpkg
  │   ├── __init__.py
  │   ├── one.py
  │   └── two_longname.py
  └── tst.py
  </code></pre>
  <p><strong>pkg/<strong>init</strong>.py</strong> is empty.</p>
  <p><strong>pkg/subpkg/<strong>init</strong>.py</strong>:</p>
  <pre><code>from pkg.subpkg.one import One
  </code></pre>
  <p><strong>pkg/subpkg/one.py</strong>:</p>
  <pre><code>import pkg.subpkg.two_longname as two
  
  class One(two.Two):
      pass
  </code></pre>
  <p><strong>pkg/subpkg/two_longname.py</strong>:</p>
  <pre><code>class Two:
      pass
  </code></pre>
  <p><strong>pkg/tst.py</strong>:</p>
  <pre><code>from pkg.subpkg import One
  
  print(One)
  </code></pre>
  <p><strong>Output</strong>:</p>
  <pre><code>$ python3.4 -m pkg.tst
  Traceback (most recent call last):
    File &quot;/usr/lib/python3.4/runpy.py&quot;, line 170, in _run_module_as_main
      &quot;__main__&quot;, mod_spec)
    File &quot;/usr/lib/python3.4/runpy.py&quot;, line 85, in _run_code
      exec(code, run_globals)
    File &quot;/home/and/dev/test/python/imptest2/pkg/tst.py&quot;, line 1, in &lt;module&gt;
      from pkg.subpkg import One
    File &quot;/home/and/dev/test/python/imptest2/pkg/subpkg/__init__.py&quot;, line 1, in &lt;module&gt;
      from pkg.subpkg.one import One
    File &quot;/home/and/dev/test/python/imptest2/pkg/subpkg/one.py&quot;, line 1, in &lt;module&gt;
      import pkg.subpkg.two_longname as two
  AttributeError: 'module' object has no attribute 'subpkg'
  </code></pre>
  <h2>Workarounds</h2>
  <p>There are changes that make it work:</p>
  <ol>
  <li><p>Empty <code>pkg/subpkg/__init__.py</code> and importing directly from <code>pkg.subpkg.one</code>.</p>
  <p>I don't consider this as an option because AFAIK &quot;lifting&quot; things to the package level is ok. Here is quote from <a href="http://mikegrouchy.com/blog/2012/05/be-pythonic-__init__py.html" rel="nofollow noreferrer">an article</a>:</p>
  <blockquote>
  <p>One common thing to do in your <code>__init__.py</code> is to import selected
  Classes, functions, etc into the package level so they can be
  conveniently imported from the package.</p>
  </blockquote>
  </li>
  <li><p>Changing <code>import as</code> to <code>from import</code> in <code>one.py</code>:</p>
  <pre><code> from pkg.subpkg import two_longname
  
   class One(two_longname.Two):
       pass
  </code></pre>
  <p>The only con here is that I can't create a short alias for module. I got that idea from @begueradj's answer.</p>
  </li>
  </ol>
  <p>It is also possible to use a relative import in <code>one.py</code> to fix the problem. But I think it's just a variation of workaround #2.</p>
  <h1>Questions</h1>
  <ol>
  <li><p>Can someone explain what is actually going on here? Why a combination of imports in <code>__init__.py</code> and usage of <code>import as</code> leads to such problems?</p>
  </li>
  <li><p>Are there any better workarounds?</p>
  </li>
  </ol>
  <hr />
  <h1>Original example</h1>
  <p>This is my original example. It's not very realistic but I'm not deleting it so @begueradj's answer still makes sense.</p>
  <p><strong>pkg/<strong>init</strong>.py</strong> is empty.</p>
  <p><strong>pkg/subpkg/<strong>init</strong>.py</strong>:</p>
  <pre><code>from pkg.subpkg.one import ONE
  </code></pre>
  <p><strong>pkg/subpkg/one.py</strong>:</p>
  <pre><code>import pkg.subpkg.two
  ONE = pkg.subpkg.two.TWO
  </code></pre>
  <p><strong>pkg/subpkg/two.py</strong>:</p>
  <pre><code>TWO = 2
  </code></pre>
  <p><strong>pkg/tst.py</strong>:</p>
  <pre><code>from pkg.subpkg import ONE
  </code></pre>
  <p><strong>Output</strong>:</p>
  <pre><code>$ python3.4 -m pkg.tst
  Traceback (most recent call last):
    File &quot;/usr/lib/python3.4/runpy.py&quot;, line 170, in _run_module_as_main
      &quot;__main__&quot;, mod_spec)
    File &quot;/usr/lib/python3.4/runpy.py&quot;, line 85, in _run_code
      exec(code, run_globals)
    File &quot;/home/and/dev/test/python/imptest/pkg/tst.py&quot;, line 1, in &lt;module&gt;
      from pkg.subpkg import ONE
    File &quot;/home/and/dev/test/python/imptest/pkg/subpkg/__init__.py&quot;, line 2, in &lt;module&gt;
      from pkg.subpkg.one import ONE
    File &quot;/home/and/dev/test/python/imptest/pkg/subpkg/one.py&quot;, line 6, in &lt;module&gt;
      ONE = pkg.subpkg.two.TWO
  AttributeError: 'module' object has no attribute 'subpkg'
  </code></pre>
  <p>Initially I had this in <strong>one.py</strong>:</p>
  <pre><code>import pkg.subpkg.two as two
  ONE = two.TWO
  </code></pre>
  <p>In that case I get error on import (just like in my original project which uses <code>import as</code> too).</p>