处理对象而不是模块重新加载的最佳方法

发布于 2024-09-11 07:46:46 字数 431 浏览 3 评论 0原文

我在 IPython 中进行了大量开发,

In[3]: from mystuff import MyObject

然后在 mystuff.py 中进行了大量更改。为了更新命名空间,我必须

In[4]: reload(mystuff)
In[5]: from mystuff import MyObject

这样做 有更好的方法吗?请注意,我无法通过直接引用 mystuff 来导入 MyObject,

In[6]: import mystuff
In[7]: mystuff.MyObject

因为这不是它在代码中的工作方式。更好的是让 IPython 在我编写文件时自动执行此操作(但这可能是另一个问题)。

任何帮助表示赞赏。

I'm doing a lot of development in IPython where

In[3]: from mystuff import MyObject

and then I make lots of changes in mystuff.py. In order to update the namespace, I have to do

In[4]: reload(mystuff)
In[5]: from mystuff import MyObject

Is there a better way to do this? Note that I cannot import MyObject by referencing mystuff directly as with

In[6]: import mystuff
In[7]: mystuff.MyObject

since that's not how it works in the code. Even better would be to have IPython automatically do this when I write the file (but that's probably a question for another time).

Any help appreciated.

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

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

发布评论

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

评论(3

薄暮涼年 2024-09-18 07:46:46

您可以使用 IPython 中的 deep_reload 功能来执行此操作。

http://ipython.scipy.org/doc/ Manual/html/interactive/reference.html?highlight=dreload

如果您使用 -deep_reload 参数运行 ipython 来替换正常的 reload() 方法。

如果这不能满足您的要求,则可以编写一个脚本来自动替换范围内的所有导入模块。虽然相当 hacky,但它应该可以工作;)

我刚刚找到了 ipy_autoreload 模块。也许这可以帮助你一点。我还不确定它是如何工作的,但这应该根据文档工作:

import ipy_autoreload
%autoreload 1

You can use the deep_reload feature from IPython to do this.

http://ipython.scipy.org/doc/manual/html/interactive/reference.html?highlight=dreload

If you run ipython with the -deep_reload parameter to replace the normal reload() method.

And if that does not do what you want, it would be possible to write a script to replace all the imported modules in the scope automatically. Fairly hacky though, but it should work ;)

I've just found the ipy_autoreload module. Perhaps that can help you a bit. I'm not yet sure how it works but this should work according to the docs:

import ipy_autoreload
%autoreload 1
我早已燃尽 2024-09-18 07:46:46

如果对象是类或函数,则可以使用其 __module__ 属性来确定要重新加载哪个模块:

def reload_for(obj):
    module = reload(__import__(obj.__module__, fromlist=True))
    return getattr(module, obj.__name__)

MyClass = reload_for(MyClass)

If the object is a class or a function, you can use its __module__ attribute to determine which module to reload:

def reload_for(obj):
    module = reload(__import__(obj.__module__, fromlist=True))
    return getattr(module, obj.__name__)

MyClass = reload_for(MyClass)
暗喜 2024-09-18 07:46:46

尝试使用 %run magic 导入您正在编辑的文件的内容。当您使用它从文件导入对象时,它会在您每次运行时更新这些对象。这样,每次回车时就不会自动重新加载。

Try using the %run magic to import the contents of the file you are editing. When you use this to import objects from a file it updates those objects every time you %run it. This way it's not autoreloading every time you carriage return.

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