Python:解决这个循环导入难题

时间:2017-01-30 00:13:03

标签: python python-import

我遇到了一个导入难题。添加新导入后,我收到以下错误

from studentApp.models import modelStudent
  File abc, line 6, in <module>
    from interviewApp.models import modelInterviewQuestion
  File "xyz", line 4, in <module>
    from mainApp.models import modelPatient
ImportError: cannot import name modelPatient

现在这就是我的文件 mainApp / models.py

from studentApp.models import modelStudent #<---Added this and I get the error

这是我的 studentApp / models.py文件

from interviewApp.models import modelInterviewQuestion #---> has a call to modelPatient inside
from mainApp.models import modelPatient 
from labApp.models import modelLabTestName #---> has a call to modelPatient inside

现在我的面试App / models.py我有这个导致循环导入

from mainApp.models import modelPatient #<---This is what is initiated the call

我理解为什么会这样,但我不知道如何解决这个问题。 有什么建议吗?

1 个答案:

答案 0 :(得分:2)

循环依赖是studentApp/models.py导入mainApp.modelsmainApp/models.py导入studentApp.models。一种解决方案是将modelPatient移入其自己的模块,然后将其导入mainApp/models.pystudentApp/models.py

相关问题