处理对象而不是模块重新加载的最佳方法
我在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 IPython 中的
deep_reload
功能来执行此操作。http://ipython.scipy.org/doc/ Manual/html/interactive/reference.html?highlight=dreload
如果您使用
-deep_reload
参数运行 ipython 来替换正常的reload()
方法。如果这不能满足您的要求,则可以编写一个脚本来自动替换范围内的所有导入模块。虽然相当 hacky,但它应该可以工作;)
我刚刚找到了
ipy_autoreload
模块。也许这可以帮助你一点。我还不确定它是如何工作的,但这应该根据文档工作: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 normalreload()
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:如果对象是类或函数,则可以使用其 __module__ 属性来确定要重新加载哪个模块:
If the object is a class or a function, you can use its
__module__
attribute to determine which module to reload:尝试使用 %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.