如何“重新导入”模块到 python 然后导入后更改代码

发布于 2024-09-30 17:10:22 字数 606 浏览 0 评论 0原文

我有一个 foo.py

def foo():
    print "test"

在 IPython 中我使用:

In [6]:  import foo
In [7]:  foo.foo()
test

然后我将 foo() 更改为:

def foo():
    print "test changed"

在 IPython 中,调用结果仍然是 test

In [10]:  import foo
In [11]:  foo.foo()
test

然后我使用:

In [15]: del foo
In [16]:  import foo
In [17]:  foo.foo()
test

我删除了同一文件夹 foo.py 中存在的 foo.pyc ,但仍然没有运气。

我可以知道如何在运行时重新导入更新的代码吗?

I have a foo.py

def foo():
    print "test"

In IPython I use:

In [6]:  import foo
In [7]:  foo.foo()
test

Then I changed the foo() to:

def foo():
    print "test changed"

In IPython, the result for invoking is still test:

In [10]:  import foo
In [11]:  foo.foo()
test

Then I use:

In [15]: del foo
In [16]:  import foo
In [17]:  foo.foo()
test

I delete the foo.pyc in same folder foo.py exists, but still no luck.

May I know how to reimport the updated code in runtime?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

倾听心声的旋律 2024-10-07 17:10:22

对于 Python 2.x

reload(foo)

对于 Python 3.x

import importlib
import foo #import the module here, so that it can be reloaded.
importlib.reload(foo)

For Python 2.x

reload(foo)

For Python 3.x

import importlib
import foo #import the module here, so that it can be reloaded.
importlib.reload(foo)
坠似风落 2024-10-07 17:10:22

IPython3 的 autoreload 功能运行得很好。

我正在使用网页上的实际示例。首先加载“自动重新加载”功能。

In []: %load_ext autoreload
In []: %autoreload 2

然后导入要测试的模块:

In []: import foo
In []: foo.some_function()
Out[]: 42

在编辑器中打开 foo.py 并将 some_function 更改为 return 43

In []: foo.some_function()
Out[]: 43

如果直接导入该函数也可以。

In []: from foo import some_function
In []: some_function()
Out[]: 42

更改 some_function 以返回 43。

In []: some_function()
Out[]: 43

IPython3's autoreload feature works just right.

I am using the actual example from the webpage. First load the 'autoreload' feature.

In []: %load_ext autoreload
In []: %autoreload 2

Then import the module you want to test:

In []: import foo
In []: foo.some_function()
Out[]: 42

Open foo.py in an editor and change some_function to return 43

In []: foo.some_function()
Out[]: 43

It also works if you import the function directly.

In []: from foo import some_function
In []: some_function()
Out[]: 42

Make change in some_function to return 43.

In []: some_function()
Out[]: 43
错爱 2024-10-07 17:10:22

除了 gnibbler 的回答之外:

这在 Python 3 中更改为:

>>> import imp
>>> imp.reload(foo)

正如 @onnodb 指出的那样,自 Python 3.4 以来,imp 已被弃用,取而代之的是 importlib

>>> import importlib
>>> importlib.reload(foo)

In addition to gnibbler's answer:

This changed in Python 3 to:

>>> import imp
>>> imp.reload(foo)

As @onnodb points out, imp is deprecated in favor of importlib since Python 3.4:

>>> import importlib
>>> importlib.reload(foo)
失退 2024-10-07 17:10:22

如果您希望这种情况自动发生,可以使用 autoreload 模块iPython 附带的。

If you want this to happen automatically, there is the autoreload module that comes with iPython.

挽梦忆笙歌 2024-10-07 17:10:22

在 [15] 中,使用 del foo

import sys
del sys.modules["foo"]

从模块缓存中删除 foo

In [15] instead of del foo, use

import sys
del sys.modules["foo"]

to delete foo from the module cache

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