文件修改后,旧代码仍在 ipython 中执行
在 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< /code>,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时导入没有再次执行呢?
In file1.py:
def foo():
import file2
print "I'm the old file1.py"
file2.bar()
if __name__ == '__main__':
foo()
In file2.py
print "I'm the old file2.py"
def bar():
print "I'm in the old file2.bar()"
On line 5 of the interactive session below, after making modifications to file1.py and file2.py changing all three occurrences of the word old
to new
, the new
code in file2.py is still not used.
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()
Where is it getting the old code from file2.py from?
I must misunderstand something, because I thought (from ipython help on run
):
The file is executed in a namespace initially consisting only of
__name__ == '__main__'
andsys.argv
constructed as indicated. It thus
sees its environment as if it were being run as a stand-alone program
I've deleted the .pyc file, and can see from the command whos
that there is no file2 module present in the namespace. But why is the import not executed again when running file1 a second time?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
run
不会启动新的 Python 进程,而是在当前进程中执行代码——不是在当前命名空间中,而是在当前的 Python 进程中,文档中的注释解释道。因此,sys.modules
仍然存在,并且使用旧的缓存模块。 (您熟悉 Python 通常缓存导入模块的方式吗?)要解决此问题,请每次都在新的 Python 进程中运行。
reload
不仅仅是一个小问题,而且可能会导致我觉得不值得的头痛。run
does not start a new Python process, but rather executes your code in the current one--not in the current namespace, but in the current Python process, the note from the documentation explains. Because of this,sys.modules
is still around and the old cached module is used. (Are you familiar with the way Python caches imported modules normally?)To fix this, run in a new Python process each time.
reload
is more than a little problematic and can lead to headaches that I find aren't worth it.我刚刚遇到这个问题,解决方案很简单
这是一个文本显示问题,一些无法显示的代码对我们来说是隐藏的,你应该做的是将所有内容复制到.txt文件中,你会看到很多旧的东西,删除它们并将代码复制回您的 file.py
old.py
但在一个新的文本文件中,您将看到:
newText.txt
我知道解释很奇怪,但这就是我看到的事实!
I just met this problem and the solution is simple
This is a text-displaying problem,some code cannot show are hidden from us,what you should do is to copy everything to a .txt file and you will see lots of old stuff,delete them and copy the code back to your file.py
old.py
But in a new text file,you will see:
newText.txt
I know the explaination is weird,but that's the truth that I saw!