文件修改后,旧代码仍在 ipython 中执行

发布于 2024-12-04 08:01:35 字数 1066 浏览 2 评论 0原文

在 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__' and sys.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浮光之海 2024-12-11 08:01:35

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.

樱花细雨 2024-12-11 08:01:35

我刚刚遇到这个问题,解决方案很简单

这是一个文本显示问题,一些无法显示的代码对我们来说是隐藏的,你应该做的是将所有内容复制到.txt文件中,你会看到很多旧的东西,删除它们并将代码复制回您的 file.py

old.py

print("Im the new one")

但在一个新的文本文件中,您将看到:

newText.txt


print("I am the old one!!!!")

#This is the old codeeeeeeeeeeeeeeeeee

print("Old codesssssssssss")


我知道解释很奇怪,但这就是我看到的事实!

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

print("Im the new one")

But in a new text file,you will see:

newText.txt


print("I am the old one!!!!")

#This is the old codeeeeeeeeeeeeeeeeee

print("Old codesssssssssss")


I know the explaination is weird,but that's the truth that I saw!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文