我試圖瞭解如何訪問和增加駐留在另一個腳本內的整數。我的層次結構是這樣的:從其他腳本遞增整數
- TestDirectory
-- foo.py
-- bar.py
例子:
foo.py
import TestDirectory.bar as bar
def main():
testCounter = 0
bar.increment()
print(testCounter)
main()
bar.py
import TestDirectory.foo as foo
def increment():
foo.main().testCounter += 1
我希望我的打印返回1,但它給了我一個錯誤:
AttributeError: module 'TestDirectory' has no attribute 'bar'
誰能解釋或解決我的問題?
無論如何,這是行不通的。但你面對的問題是我認爲的循環進口。 –
除了循環導入問題外,我無法重現此錯誤。當我嘗試從TestDirectory外部運行它時,我得到一個名爲'TestDirectory'的'ImportError:No'模塊。 –
但是你有一個想法如何解決這樣的問題? – Anonymous