My team is working on huge project with Django. For sake of simplicity, here's plain Python to illustrate the problem (original problem has models and apps instead of classes (I know that both are classes) and packages (I know that both are packages as well)).
a.py:
from b import B1
class A1(object):
def __init__(self):
print "object A1"
class A2(object):
def __init__(self):
print "object A2"
A1()
B1()
b.py:
from a import A2
class B1(object):
def __init__(self):
print "object B1"
A2()
When a.py is called, it tries to import B1 from b package which, on the other hand, tries to import A2 from a package and then, from start, that repeats forever. Python says:
[dagrevis@localhost recursion]$ python2 a.py
Traceback (most recent call last):
File "a.py", line 1, in <module>
from b import B1
File "/home/dagrevis/Python/recursion/b.py", line 1, in <module>
from a import A2
File "/home/dagrevis/Python/recursion/a.py", line 1, in <module>
from b import B1
ImportError: cannot import name B1
One of the solutions would be to have one file per object. Something like C++ or Java has. Then I remembered what Guido said about Python: “Don't write Java (or C++, or Javascript, ...) in Python.“. Are there more Pythonic way to deal with this problem?
Thanks in any advice!