擅长:python、mysql、java
<h2>回答</h2>
<p>从我的测试中。有标记的答案表示简单的<code>reload(X)</code>,但不起作用。</p>
<p>据我所知,正确答案是:</p>
<pre><code>from importlib import reload # python 2.7 does not require this
import X
reload( X )
from X import Y
</code></pre>
<hr/>
<h2>测试</h2>
<p>我的测试如下(Python 2.6.5+bpython 0.9.5.2)</p>
<p>X.py公司:</p>
<pre><code>def Y():
print "Test 1"
</code></pre>
<p>布莱顿:</p>
<pre><code>>>> from X import Y
>>> print Y()
Test 1
>>> # Edit X.py to say "Test 2"
>>> print Y()
Test 1
>>> reload( X ) # doesn't work because X not imported yet
Traceback (most recent call last):
File "<input>", line 1, in <module>
NameError: name 'X' is not defined
>>> import X
>>> print Y()
Test 1
>>> print X.Y()
Test 1
>>> reload( X ) # No effect on previous "from" statements
>>> print Y()
Test 1
>>> print X.Y() # first one that indicates refresh
Test 2
>>> from X import Y
>>> print Y()
Test 2
>>> # Finally get what we were after
</code></pre>