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>EDIT 2: Added code to class2.py to attach the parent directory to the PYTHONPATH to comply with how Python3 module imports work.</p>

<pre><code>import sys
import os
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
</code></pre>

<p>Removed relative import of Class1.</p>

<p>Folder structure:</p>

<pre><code>project2
  - class3.py
  - project1
    - __init__.py
    - class1.py
    - class2.py
</code></pre>

<p><em>project2/project1/class1.py</em></p>

<pre><code>class Class1(object):
    def __init__(self):
        super(Class1, self).__init__()
        self.name = "DAVE!"

    def printname(self):
        print(self.name)

def run():
    thingamy = Class1()
    thingamy.printname()

if __name__ == "__main__":
    run()
</code></pre>

<p><em>project2/project1/class2.py</em></p>

<pre><code>import sys
import os
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

from class1 import Class1

class Class2(Class1):
    def childMethod(self):
      print('Calling child method')

def run():
    thingamy = Class2()
    thingamy.printname()
    thingamy.childMethod()

if __name__ == "__main__":
    run()
</code></pre>

<p><em>project2/class3.py</em></p>

<pre><code>from project1.class2 import Class2
from project1.class1 import Class1

class Class3(Class2):
    def anotherChildMethod(self):
      print('Calling another child method')

def run():
    thingamy = Class3()
    thingamy.printname()
    thingamy.anotherChildMethod()

if __name__ == "__main__":
    run()
</code></pre>

<p>With this setup each of class1, 2 and 3 can be run as standalone scripts.</p>

python python-3.x package python-import