Python - Intra-package importing when package modules are sometimes used as standalone?
<p>Sorry if this has already been answered using terminology I don't know to search for.</p>

<p>I have one project:</p>

<pre><code>project1/
    class1.py
    class2.py
</code></pre>

<p>Where <code>class2</code> imports some things from <code>class1</code>, but each has its own <code>if __name__ == '__main__'</code> that uses their respective classes I run frequently. But then, I have a second project which creates a subclass of each of the classes from <code>project1</code>. So I would like <code>project1</code> to be a package, so that I can import it into <code>project2</code> nicely:</p>

<pre><code>project2/
    project1/
        __init__.py
        class1.py
        class2.py
    subclass1.py
    subclass2.py
</code></pre>

<p>However, I'm having trouble with the importing with this. If I make <code>project1</code> a package then inside <code>class2.py</code> I would want to import <code>class1.py</code> code using <code>from project1.class1 import class1</code>. This makes <code>project2</code> code run correctly. But now when I'm trying to use <code>project1</code> not as a package, but just running code from directly within that directory, the <code>project1</code> code fails (since it doesn't know what <code>project1</code> is). If I set it up for <code>project1</code> to work directly within that directory (i.e. the import in <code>class2</code> is <code>from class1 import Class1</code>), then this import fails when trying to use <code>project1</code> as a package from <code>project2</code>.</p>

<p>Is there a way to have it both ways (use <code>project1</code> both as a package and not as a package)? If there is a way, is it a discouraged way and I should be restructuring my code anyway? Other suggestions on how I should be handling this? Thanks!</p>

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

<p>Just to clarify, the problem arrises because <code>subclass2</code> imports <code>class2</code> which in turn imports <code>class1</code>. Depending on which way <code>class2</code> imports <code>class1</code> the import will fail from <code>project2</code> or from <code>project1</code> because one sees <code>project1</code> as a package while the other sees it as the working directory.</p>

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

<p>I'm using Python 3.5. Apparently this works in Python 2, but not in my current version of python.</p>

<p>You could run <code>class2.py</code> from inside the <code>project2</code> folder, i.e. with the current working directory set to the <code>project2</code> folder:</p>

<pre><code>user@host:.../project2$ python project1/class2.py
</code></pre>

<p>On windows that would look like this:</p>

<pre><code>C:\...project2&gt; python project1/class2.py
</code></pre>

<p>Alternatively you could modify the python path inside of <code>class2.py</code>:</p>

<pre><code>import sys
sys.path.append(".../project2")

from project1.class1 import class1
</code></pre>

<p>Or modify the <code>PYTHONPATH</code> environment variable similarly.</p>

<p>To be able to extend your project and import for example something in <code>subclass1.py</code> from <code>subclass2.py</code> you should consider starting the import paths always with <code>project2</code>, for example in <code>class2.py</code>:</p>

<pre><code>from project2.project1.class1 import class1
</code></pre>

<p>Ofcourse you would need to adjust the methods I just showed to match the new path.</p>

python python-3.x package python-import