title: Python import error when package is being imported
body: <p>I have a strange (for me) problem with python imports and I would like to understand what is wrong with it. </p>
  
  <p>I have created a demo project with the following directory structure:</p>
  
  <pre><code>├── entry.py
  └── foo
      ├── __init__.py
      └── bar
          ├── __init__.py
          ├── sdk.py
          ├── service.py
          └── utils.py
  </code></pre>
  
  <p>If I run <code>python entry.py</code>, in <code>service.py</code> the following statement works:</p>
  
  <pre><code>from foo.bar import utils
  </code></pre>
  
  <p>while this does not:</p>
  
  <pre><code>import foo.bar.utils as utils
  </code></pre>
  
  <pre><code># entry.py:
  
  from foo.bar.sdk import get_sdk
  print(get_sdk())
  
  # foo - __init__.py
  # empty on purpose
  
  # bar - __init__.py
  from .service import Service
  
  # sdk.py
  def get_sdk():
      return "SDK"
  
  # service.py
  import foo.bar.utils as utils # wrong import
  # from foo.bar import utils # works!
  class Service:
      def do_service(self):
          util = utils.get_utility_string()
          print("Doing service call {}".format(util))
  
  # utils.py
  def get_utility_string():
      return "This is useful"
  
  </code></pre>
  
  <p>By running <code>python entry.py</code> I get:</p>
  
  <pre><code>Traceback (most recent call last):
    File "entry.py", line 1, in &lt;module&gt;
      from foo.bar.sdk import get_sdk
    File "/Users/paloand/Downloads/p_demo/foo/bar/__init__.py", line 1, in &lt;module&gt;
      from .service import Service
    File "/Users/paloand/Downloads/p_demo/foo/bar/service.py", line 1, in &lt;module&gt;
      import foo.bar.utils as utils
  AttributeError: module 'foo' has no attribute 'bar'
  
  </code></pre>
  
  <p>From my understanding in <code>entry.py</code> when the <code>get_sdk</code> symbol is getting imoprted from <code>sdk</code> module, <code>foo</code> package is being imported as well. While importing <code>foo</code> in <code>bar</code> the <code>__init__.py</code> file requests the import of the <code>Service</code> symbol. So while <code>foo</code> is being imported it is required again in <code>service.py</code>. </p>
  
  <p>Link to the uploaded demo project in my Dropbox: <a href="https://www.dropbox.com/s/ozwvol07kc3sl8f/p_demo.zip?dl=0" rel="nofollow noreferrer">Demo</a></p>
  
  <p>I have read <a href="https://stackoverflow.com/questions/710551/use-import-module-or-from-module-import">this</a> question and all the answer but there I could not extract any information there. I also found this <a href="https://bugs.python.org/issue992389" rel="nofollow noreferrer">bug</a> but I am not sure if it applies here. I read also the <a href="http://python-notes.curiousefficiency.org/en/latest/python_concepts/import_traps.html" rel="nofollow noreferrer">common traps</a> of import in python but could not find this one.</p>
  
  <p>I want to understand the difference and what is really happening.</p>
  
  <p>I am using <strong>python 3.6</strong></p>
  
  <p>Thank you</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Updated with error message</p>
  
  <p><strong>EDIT 2</strong></p>
  
  <p>I have tried <code>python -vv entry.py</code> in both cases. The relevant logs are in the paste: <a href="https://pastebin.com/J9guMqJS" rel="nofollow noreferrer">https://pastebin.com/J9guMqJS</a>.</p>
  
  <p>In both cases the import order is the same, in the "bad" case this import  fails:</p>
  
  <pre><code>import 'foo.bar.utils' # &lt;_frozen_importlib_external.SourceFileLoader object at 0x1103ddf28&gt;
  </code></pre>