我有两行代码:
import ui.interface.interface
obj = ui.interface.interface.InterfaceCodes()
我在另一个包中有相同的代码并且工作正常。但我得到以下追溯:
Traceback (most recent call last):
File "./main.py", line 31, in <module>
from materials.materials import *
File "/home/mohsen/codes/amlak/amlak/src/materials/materials.py", line
40, in <module>
from ui.interface.interface import *
File "/home/mohsen/codes/amlak/amlak/src/ui/interface/interface.py",
line 32, in <module>
from ui.materialsFrame import *
File "/home/mohsen/codes/amlak/amlak/src/ui/materialsFrame.py", line
24, in <module>
from ui.materialsFindFrame import *
File "/home/mohsen/codes/amlak/amlak/src/ui/materialsFindFrame.py",
line 14, in <module>
from common.objects.objects import *
File "/home/mohsen/codes/amlak/amlak/src/common/objects/objects.py",
line 28, in <module>
obj = ui.interface.interface.InterfaceCodes()
AttributeError: 'module' object has no attribute 'interface'
当我将我的代码更改为:
from ui.interface.interface import *
obj = ui.interface.interface.InterfaceCodes()
我得到了:
Traceback (most recent call last):
File "./main.py", line 31, in <module>
from materials.materials import *
File "/home/mohsen/codes/amlak/amlak/src/materials/materials.py", line
40, in <module>
from ui.interface.interface import *
File "/home/mohsen/codes/amlak/amlak/src/ui/interface/interface.py",
line 32, in <module>
from ui.materialsFrame import *
File "/home/mohsen/codes/amlak/amlak/src/ui/materialsFrame.py", line
24, in <module>
from ui.materialsFindFrame import *
File "/home/mohsen/codes/amlak/amlak/src/ui/materialsFindFrame.py",
line 14, in <module>
from common.objects.objects import *
File "/home/mohsen/codes/amlak/amlak/src/common/objects/objects.py",
line 28, in <module>
Obj = ui.interface.interface.InterfaceCodes()
NameError: name 'ui' is not defined
此外,如果我删除“ui.interface.interface”,我会得到:
Traceback (most recent call last):
File "./main.py", line 31, in <module>
from materials.materials import *
File "/home/mohsen/codes/amlak/amlak/src/materials/materials.py", line
40, in <module>
from ui.interface.interface import *
File "/home/mohsen/codes/amlak/amlak/src/ui/interface/interface.py",
line 32, in <module>
from ui.materialsFrame import *
File "/home/mohsen/codes/amlak/amlak/src/ui/materialsFrame.py", line
24, in <module>
from ui.materialsFindFrame import *
File "/home/mohsen/codes/amlak/amlak/src/ui/materialsFindFrame.py",
line 14, in <module>
from common.objects.objects import *
File "/home/mohsen/codes/amlak/amlak/src/common/objects/objects.py",
line 28, in <module>
Obj = InterfaceCodes()
NameError: name 'InterfaceCodes' is not defined
我的问题是,我真的不知道怎么做......!
答案 0 :(得分:1)
您似乎遇到了循环导入问题。通过第一个堆栈跟踪的外观,
ui.interface.interface
导入ui.materialsFrame
,ui.materialsFrame
导入ui.materialsFindFrame
,ui.materialsFindFrame
导入common.objects.objects
,common.objects.objects
导入ui.interface.interface
, ...已经在导入过程中了。由于ui.interface.interface
尝试在开始时尝试导入ui.materialsFrame
,因此在上次导入时,其变量中还没有InterfaceCodes
,这就是您获得AttributeError
的原因。
尝试重新设计你的库以摆脱这个循环,或者至少尝试在导入时不要实际调用函数/构造函数。