How do I structure my Python project to allow named modules to be imported from sub directories
<p>This is my directory structure:</p>

<pre><code>Projects
    + Project_1
    + Project_2
    - Project_3
        - Lib1
            __init__.py # empty
            moduleA.py
        - Tests
            __init__.py # empty
            foo_tests.py
            bar_tests.py
            setpath.py
        __init__.py     # empty
        foo.py
        bar.py
</code></pre>

<p>Goals:</p>

<ol>
<li>Have an organized project structure</li>
<li>Be able to independently run each .py file when necessary</li>
<li>Be able to reference/import both sibling and <strong>cousin</strong> modules</li>
<li>Keep all import/from statements at the beginning of each file.</li>
</ol>

<p>I Achieved #1 by using the above structure</p>

<p>I've mostly achieved 2, 3, and 4 by doing the following (as recommended by <a href="http://docs.python-guide.org/en/latest/writing/structure/">this excellent guide</a>)</p>

<p>In any package that needs to access parent or cousin modules (such as the Tests directory above) I include a file called setpath.py which has the following code:</p>

<pre><code>import os
import sys
sys.path.insert(0, os.path.abspath('..'))

sys.path.insert(0, os.path.abspath('.'))
sys.path.insert(0, os.path.abspath('...'))
</code></pre>

<p>Then, in each module that needs parent/cousin access, such as foo_tests.py, I can write a nice clean list of imports like so:</p>

<pre><code>import setpath      # Annoyingly, PyCharm warns me that this is an unused import statement
import foo.py
</code></pre>

<p>Inside setpath.py, the second and third inserts are not strictly necessary for this example, but are included as a troubleshooting step.</p>

<p>My problem is that this only works for imports that reference the module name directly, and not for imports that reference the package.  For example, inside bar_tests.py, neither of the two statements below work when running bar_tests.py <strong>directly</strong>.</p>

<pre><code>import setpath

import Project_3.foo.py  # Error
from Project_3 import foo  # Error
</code></pre>

<p>I receive the error "ImportError: No module named 'Project_3'".  </p>

<p>What is odd is that I can run the file directly from within PyCharm and it works fine. I know that PyCharm is doing some behind the scenes magic with the <code>Python Path</code> variable to make everything work, but I can't figure out what it is.  As PyCharm simply runs python.exe and sets some environmental variables, it should be possible to clone this behavior from within a Python script itself. </p>

<p>For reasons not really germane to this question, I have to reference <code>bar</code> using the <code>Project_3</code> qualifier.</p>

<p>I'm open to any solution that accomplishes the above while still meeting my earlier goals. I'm also open to an alternate directory structure if there is one that works better. I've read the <a href="https://docs.python.org/3/reference/import.html">Python doc on imports</a> and packages but am still at a loss. I think one possible avenue might be manually setting the <code>__path__</code> variable, but I'm not sure which one needs to be changed or what to set it to.</p>