【发布时间】:2011-11-17 03:02:32
【问题描述】:
在file1.py中:
def foo():
import file2
print "I'm the old file1.py"
file2.bar()
if __name__ == '__main__':
foo()
在file2.py中
print "I'm the old file2.py"
def bar():
print "I'm in the old file2.bar()"
在下面的交互会话的第 5 行,在对 file1.py 和 file2.py 进行修改后,将所有三个出现的单词 old 更改为 new,file2.py 中的 new 代码仍然没有用过的。
wim@wim-ubuntu:~/sandpit$ ipython
>>> run file1.py
I'm the old file2.py
I'm the old file1.py
I'm in the old file2.bar()
>>> !rm file2.pyc
>>> # modify file1, file2
>>> run file1.py
I'm the new file1.py
I'm in the old file2.bar()
file2.py 中的旧代码是从哪里获取的?
我一定是误解了一些东西,因为我认为(来自run 上的 ipython 帮助):
文件在最初仅由以下内容组成的命名空间中执行
__name__ == '__main__'和sys.argv构造如所示。它因此 将其环境视为作为独立程序运行
我已经删除了 .pyc 文件,并且可以从命令whos 中看到命名空间中不存在 file2 模块。但是为什么第二次运行file1时没有再次执行导入?
【问题讨论】:
-
IPython.lib.dreload(或 0.10.x 中的IPython.deep_reload)中的“深度重载”可能会帮助您。 -
如果有人有类似的情况:我虽然有类似的问题,但我使用常规 python 运行代码(不是在解释器模式下)。即使删除
.pyc文件也无济于事。事实证明,当我向上滚动(使用 Vi 插件)时,它只是 Tmux 向我显示旧输出。 -
你基本上每次都运行一个新的
ipython会话吗?
标签: python import module namespaces ipython