-2

我有两行代码:

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

的问题是,我真的不知道该怎么做......!

4

1 回答 1

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.

尝试重新设计您的库以摆脱此循环,或者至少尝试在导入时不实际调用函数/构造函数。

于 2013-09-16T03:06:53.627 回答