3

我制作了两个文件:

#test_func.py
def test():
    print('hello')

#test_inspect.py
import inspect
import test_func

reload(inspect)
reload(test_func)
reload(inspect)
reload(test_func)

print inspect.getsource(test_func.test)

import test_inspect从 IPython 或其他交互式 shell运行会打印正确的内容:

def test():
    print('hello')

但如果我编辑并将 test_func.py 保存为:

#test_func.py
def test():
    print('good bye')

我仍然得到:

def test():
    print('hello')

当我reload(test_inspect)从外壳运行时。如何说服inspect模块重新读取源文件?

(如果您必须知道我为什么要这样做,我会在评论中详细说明,但是现在,我只想知道是否有解决方法,或者检查模块是否有一些基本的东西可以防止这个)

4

1 回答 1

5

这应该可以解决问题:

import linecache
linecache.clearcache()
于 2013-05-07T00:48:06.123 回答