title: Import a module from both within same package and from outside the package in Python 3
body: <p>Okay, the scenario is very simple. I have this file structure:</p>
  
  <pre><code>.
  ├── interface.py
  ├── pkg
  │   ├── __init__.py
  │   ├── mod1.py
  │   ├── mod2.py
  </code></pre>
  
  <p>Now, these are my conditions:</p>
  
  <ul>
  <li>mod2 needs to import mod1.</li>
  <li>both interface.py and mod2 needs to be run independently as a main script. If you want, think interface as the actual program and mod2 as an internal tester of the package.</li>
  </ul>
  
  <p>So, in Python 2 I would simply do <code>import mod1</code> inside mod2.py and both <code>python2 mod2.py</code> and <code>python2 interface.py</code> would work as expected.</p>
  
  <p>However, and this is the part I less understand, using Python 3.5.2, if I do <code>import mod1</code>; then I can do <code>python3 mod2.py</code>, but <code>python3 interface.py</code> throws: <code>ImportError: No module named 'mod1'</code> :(</p>
  
  <p>So, apparently, python 3 proposes to use <code>import pkg.mod1</code> to avoid collisions against built-in modules. Ok, If I use that I can do <code>python3 interface.py</code>; but then I can't <code>python3 mod2.py</code> because: <code>ImportError: No module named 'pkg'</code></p>
  
  <p>Similarly, If I use relative import:
  <code>from . import mod1</code> then <code>python3 interface.py</code> works; but mod2.py says <code>SystemError: Parent module '' not loaded, cannot perform relative import</code> :( :(</p>
  
  <p>The only "solution", I've found is to go up one folder and do <code>python -m pkg.mod2</code> and then it works. But do we have to be adding the package prefix <code>pkg</code> to every import to other modules within that package? Even more, to run any scripts inside the package, do I have to remember to go one folder up and use the -m switch? That's the only way to go??</p>
  
  <p>I'm confused. This scenario was pretty straightforward with python 2, but looks awkward in python 3.</p>
  
  <p>UPDATE: I have upload those files with the (referred as "solution" above) working source code here: <a href="https://gitlab.com/Akronix/test_python3_packages" rel="noreferrer">https://gitlab.com/Akronix/test_python3_packages</a>. Note that I still don't like it, and looks much uglier than the python2 solution.</p>
  
  <hr>
  
  <p>Related SO questions I've already read:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/10647729/python-import-the-package-in-a-module-that-is-inside-the-same-package">Python -- import the package in a module that is inside the same package</a></li>
  <li><a href="https://stackoverflow.com/questions/72852/how-to-do-relative-imports-in-python">How to do relative imports in Python?</a></li>
  <li><a href="https://stackoverflow.com/questions/23536163/absolute-import-module-in-same-package#235400517">Absolute import module in same package</a></li>
  </ul>
  
  <p>Related links:</p>
  
  <ul>
  <li><a href="https://docs.python.org/3.5/tutorial/modules.html" rel="noreferrer">https://docs.python.org/3.5/tutorial/modules.html</a></li>
  <li><a href="https://www.python.org/dev/peps/pep-0328/" rel="noreferrer">https://www.python.org/dev/peps/pep-0328/</a></li>
  <li><a href="https://www.python.org/dev/peps/pep-0366/" rel="noreferrer">https://www.python.org/dev/peps/pep-0366/</a></li>
  </ul>