Reputation: 954
I am struggeling with resolving a circular import issue. My setup is as follows:
Module a.py
:
import b
class A():
pass
Module b.py
:
import a
class Bar(a.A):
pass
Notice the circular import in module b
: is needs to access class A
from module a
, because B
inherits from A
.
Finally, the main module, main.py
:
import a
# ...
When I run main.py
, I get this error:
$ python3 main.py
Traceback (most recent call last):
File "/home/.../main.py", line 1, in <module>
import a
File "/home/.../a.py", line 1, in <module>
import b
File "/home/.../b.py", line 3, in <module>
class Bar(a.A):
AttributeError: partially initialized module 'a' has no attribute 'A' (most likely due to a circular import)
I thought, one way of resolving circular imports was to not use the from ... import ...
statement, which I didn't do as you can see. But why am I still getting that error then?
EDIT: My current code structure requires that A and B are separate modules. Nevertheless, in this video, he is using a similar structure as mine, and he has no problems...
Upvotes: 1
Views: 1777
Reputation: 18792
The ideal answer in such cases is usually to
import a
import b
class Bar(a.A):
pass
Upvotes: 1