Okay, I do understand that this topic is old as hell, but I couldn't find an answer to the particular question that I am asking.
Let's say that we have a very simple structure: two files, a.py
and b.py
, their contents being:
a.py
import bclass C:lal = 4class A:kek = 12lol = b.B()
b.py
import aclass B:aa = a.C()
Trying to run python b.py
, we get:
Traceback (most recent call last):File "b.py", line 1, in <module>import aFile ".../a.py", line 1, in <module>import bFile ".../b.py", line 3, in <module>class B:File ".../a.py", line 5, in A aa = a.C()
AttributeError: module 'a' has no attribute 'C'
BUT if we move the import b
line AFTER the C
class, the script launches and produces no errors.
I have not found any mention of this whatsoever in any answers here on SO. The question here is: Why does this happen and how to escape this?
This is a particularly important question for the Django framework. When I have many models, I try splitting them into many files. It is very easy to get a cyclic import there.