嗯,我正在尝试设置具有 fileB
函数的 fileA
。在文件 B
中,我使用了文件 A
中的一些变量。事情是这样的:
fileA
import fileB
a = []
fileB.function1()
文件 B 是:
fileB
import fileA
def function1():
fileA.a.extend([2, 3])
但我收到此错误:
AttributeError: module 'fileB' has no attribute 'function1'
我知道有很多关于同一件事的问题,但我还没有看到任何人有这样的错误,直到现在我无法找到解决方案
最佳答案
在这种情况下,您可以使用本地导入而不是全局导入。我在 OpenStack 的源代码中看到过很多这样的内容。
f1.py
import f2
a = []
f2.function1()
f2.py
def function1():
import f1
f1.a.extend([2, 3])
关于python - Python 上的循环导入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59878923/