Unable to import modules from relative path
<p>I have a problem for importing modules from relative path with Python. I tried all I found on the web. Here is my directory structure:</p>

<pre><code>starcipher/
    __init__.py
    caesar.py
    tests/
        __init__.py
        test_caesar.py
</code></pre>

<p>As you can tell, the <code>tests/</code> directory contains all my unit tests. The <code>test_caesar.py</code> uses the class defined in <code>caesar.py</code>. Here is my files:</p>

<p><code>caesar.py</code>:</p>

<pre><code>class Caesar:
    # Blabla
</code></pre>

<p><code>tests/test_caesar.py</code>:</p>

<pre><code>import unittest
from ..caesar import Caesar

# I also tried:
from caesar import Caesar
from starcipher.caesar import Caesar
from . import Caesar
from .. import Caesar

# Nothing works.

class TestCaesar(unittest.TestCase):
   # Blabla
</code></pre>

<p>I have this error each time:</p>

<pre><code>Traceback (most recent call last):
  File "test_caesar.py", line 2, in &lt;module&gt;
    from ..caesar import Caesar
SystemError: Parent module '' not loaded, cannot perform relative import
</code></pre>

<p><strong>EDIT</strong></p>

<p>Here is how I run my unit test:</p>

<ul>
<li>In the root directory: <code>python -m unittest discover tests/</code></li>
<li>Or in the <code>tests/</code> directory: <code>python test_caesar.py</code></li>
<li>Or even: <code>python -m unittest</code></li>
</ul>

<p><strong>SOLUTION</strong></p>

<p>Thanks to Pocin, removing the <code>__init__.py</code> file from the <code>tests/</code> directory solved the problem!</p>

<p>Thank you.</p>

<p>Just so the solution is nicely visible, the fix is to delete <code>tests/__init__.py</code> file.</p>

<p>However I am not really sure why that works and It would be great if someone could provide explanation.</p>

python import module