Possible Duplicate:
Circular (or cyclic) imports in Python
a.py
import b
class Abstract(object):
pass
class Concrete(Abstract):
def get_newthing(self):
return b.NewThing()
(Note: It will be difficult for me to do any major refactoring of a.py)
b.py
import a
#reload(a)
class NewThing(a.Abstract):
pass
As written, running "import b, a" works, but running "import a" gives
AttributeError: 'module' object has no attribute 'Abstract'
as Python reaches the "import b" line in a.py and then while importing b tries to access "a.Abstract" which hasn't been created yet.
If I include the reload statement though, I can do "import a" just fine, as Python jumps back to the a.py module and creates the Abstract class before continuing in b.py. So it seems to work (although I should probably add a hasattr check before doing the reload).
I have been looking for ways to resolve this import loop issue and haven't seen any suggestions along these lines. Is there any pitfall in using reload() in this way?