I have a parent and child class, where a parent's method returns an instance of the child. Both classes are in separate files classA.py
and classB.py
. In order to avoid circular imports when I import classA
I added the classB
import to the end of classA.py
(as shown below). Everything worked well and I was able to properly use classA
in my code.
Now I'm having issues if I want to use ONLY classB
. For example, if I run
from classB import ClassB
I get the following error:
File "classA.py", line 269, in <module>
from classB import ClassB
ImportError: cannot import name ClassB
If I run:
from classA import ClassA
from classB import ClassB
then everything works perfectly and I can use both classes. Is there a way to only import classB
or must I ALWAYS first import classA
and then classB
?
classA.py
class ClassA():
def __init__(self, ...):
....
def someMethod(self, ...):
...
return ClassB(...)
from classB import ClassB
classB.py
from classA import ClassA
class ClassB(ClassA):
def __init__(self, ...):
super(ClassB, self).__init__(...)