title: Python sub-sub-module: "import as" vs "import"
body: <p>I'm working in Python 2.7. </p>
  
  <p>This is my folder structure. (<code>temp</code> is already added to this system path.)</p>
  
  <pre><code>temp
  |
  |--main.py
  |
  |--sub
     |
     |--__init__.py
     |
     |--sub2
        |
        |--__init__.py
        |
        |--square.py
  </code></pre>
  
  <p>The file contents are listed below. </p>
  
  <pre><code>main.py:
  
  import sub.sub2 as sub2
  sub2.run()
  </code></pre>
  
  
  
  <pre><code>sub/__init__.py: empty
  </code></pre>
  
  
  
  <pre><code>sub/sub2/__init__.py:
  
  import sub.sub2.square as square
  def run():
      square.square_it(3)
  </code></pre>
  
  
  
  <pre><code>sub/sub2/square.py:
  
  def square_it(x): return x**2
  </code></pre>
  
  <p>When I execute <code>main.py</code>, I receive the following error (ignore the line numbers):</p>
  
  <pre><code>Traceback (most recent call last):
    File "main.py", line 3, in &lt;module&gt;
      import sub.sub2 as sub2
    File "/home/gimlisonofgloin1/temp/sub/sub2/__init__.py", line 3, in &lt;module&gt;
      import sub.sub2.square as square
  AttributeError: 'module' object has no attribute 'sub2'
  </code></pre>
  
  <p>I can fix this by changing the statement in which the error occurs to any of these statements (in the final three listed solutions, I have to change the function call appropriately, of course):</p>
  
  <ul>
  <li><code>from sub.sub2 import square as square</code>;</li>
  <li><code>from sub.sub2.square import square_it</code>;</li>
  <li><code>from .square import square_it</code> (as pointed out kindly in user NeErAj KuMaR's answer); or</li>
  <li><code>import sub.sub2.square</code>.</li>
  </ul>
  
  <p>My question is: <strong>why does the original line of code yield an error, even though it is semantically equivalent to the working ("fixed") lines of code (in particular, the 1st and 4th solutions listed)?</strong></p>
  
  <p>In attempting to answer this question, I have stumbled across this bit of text from the <a href="https://docs.python.org/2.0/ref/import.html" rel="nofollow noreferrer">Python 2.0 Reference Manual</a>:</p>
  
  <blockquote>
    <p>To avoid confusion, you cannot import sub-modules 'as' a different local name. So 'import module as m' is legal, but 'import module.submod as s' is not. The latter should be written as 'from module import submod as s', see below.</p>
  </blockquote>
  
  <p>This is consistent with the errors I'm receiving. However, this (seemingly important) little blurb is not present anywhere in the <a href="https://docs.python.org/2/reference/simple_stmts.html#the-import-statement" rel="nofollow noreferrer">Python 2.7 Reference Manual</a>. Is that little blurb from the Python 2.0 reference still applicable in Python 2.7? Or am I getting this error for a completely different reason that I'm not aware of?</p>