title: Python 3.5 "ImportError: cannot import name 'SomeName'
body: <p>I am trying to implement a small library for Python 3.5 but keep struggling with how to correctly handle the structuring of the packages/modules and how to get the imports to work.</p>
  
  <p>I keep running into the problem where python complains of being unable to import some name with an error like</p>
  
  <pre><code>ImportError: cannot import name 'SubClass1'
  </code></pre>
  
  <p>This seems to happen when "SubClass1" needs to import some other module but that other module also needs to know about SubClass1 (a cyclic import).
  I need the cyclic import in my library because the base class has a factory method that creates the proper subclass instances (there are also other situations where cyclic imports are needed, e.g. checking the type of a function argument needs the import of where that type is defined, but that module may itself need the class where that check is done: another cyclic dependency!)</p>
  
  <p>Here is example code:</p>
  
  <p>Root directory contains the subdirectory dir1. The directory dir1 contains and empty file <strong>init</strong>.py, a file baseclass.py and a file subclass1.py.
  The file ./dir1/subclass1.py contains:</p>
  
  <pre><code>from . baseclass import BaseClass
  class SubClass1(BaseClass):
     pass
  </code></pre>
  
  <p>The file ./dir1/baseclass.py contains:</p>
  
  <pre><code>from . subclass1 import SubClass1
  class BaseClass(object):
     def make(self,somearg):
        # .. some logic to decide which subclass to create
        ret = SubClass1()
        # .. which gets eventually returned by this factory method
        return ret
  </code></pre>
  
  <p>The file ./test1.py  contains:</p>
  
  <pre><code>from dir1.subclass1 import SubClass1
  sc1 = SubClass1()
  </code></pre>
  
  <p>This results in the following error:</p>
  
  <pre><code>Traceback (most recent call last):
    File "test1.py", line 1, in &lt;module&gt;
       from dir1.subclass1 import SubClass1
    File "/data/johann/tmp/python1/dir1/subclass1.py", line 1, in &lt;module&gt;
      from . baseclass import BaseClass
    File "/data/johann/tmp/python1/dir1/baseclass.py", line 1, in &lt;module&gt;
      from . subclass1 import SubClass1
  ImportError: cannot import name 'SubClass1'
  </code></pre>
  
  <p>What is the standard/best way to solve this problem, ideally in a way that is backwards compatible to python 2.x and python 3 up to version 3.2? </p>
  
  <p>I have read elsewhere that importing the module instead of something from a module may help here but I do not know how to just import the module (e.g. subclass1) in a relative way because "import . subclass1" or similar does not work.</p>