擅长:python、mysql、java
<p>你的代码有很多问题:</p>
<ol>
<li>方法中的变量是方法的本地变量,您不能从函数外部访问它们,忘记脚本外部的变量(即模块)</li>
<li>要在同一文件夹中导入另一个模块,只需使用脚本本身的名称</li>
<li>由于您希望从<code>bar</code>访问<code>foo</code>,并且从<code>foo</code>访问<code>bar</code>,因此您最终会得到一个循环导入,本地导入可以避免这种情况</li>
</ol>
<p>这里有一个解决你问题的方法,但很可能,你会做得更好的设计更改,而不是我所提供的:</p>
<p>你知道吗foo.py公司你知道吗</p>
<pre><code>import bar
testCounter=0
if __name__=="__main__":
bar.incrementTestCounter()
print bar.getTestCounterValue()
</code></pre>
<p>你知道吗棒.py你知道吗</p>
<pre><code>def incrementTestCounter():
import foo
foo.testCounter=foo.testCounter+1
def getTestCounterValue():
import foo
return foo.testCounter
</code></pre>