从IPython重新加载Python扩展模块 [英] Reloading a Python extension module from IPython

查看:479
本文介绍了从IPython重新加载Python扩展模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Cython,我正在开发一个扩展模块,它将构建为.so文件。然后我使用IPython测试它。在开发过程中,我经常需要进行更改和重建。我还需要退出IPython shell并重新输入所有命令。重新导入模块

Using Cython, I am developing an extension module which gets build as an .so file. I then test it using IPython. During development, I frequently need to make changes and rebuild. I also need to exit the IPython shell and reenter all commands. Reimporting the module with

import imp
imp.reload(Extension)

不起作用,代码未更新。在重建模块后,有没有办法避免重启IPython shell?

does not work, the code is not updated. Is there a way for me to avoid restarting the IPython shell after I rebuild the module?

推荐答案

如果不重新启动,则无法重新加载C扩展名该过程(有关详细信息,请参阅此官方Python错误)。

C extensions cannot be reloaded without restarting the process (see this official Python bug for more info).

由于您已经在使用IPython,我建议您使用其中一个双进程接口,例如Notebook或QtConsole,如果您可以接受的话。这些允许您轻松重新启动内核进程,从而允许您重新加载模块。显然,这不如Python模块的重载方便,因为你必须重新执行才能返回到相同的状态。但这是不可避免的,所以重点是减轻不便。

Since you are already using IPython, I might recommend using one of the two-process interfaces such as the Notebook or QtConsole, if it's acceptable to you. These allow you to easily restart the kernel process, which allows to you load the module anew. Obviously, this isn't as convenient as reload for a Python module because you have to re-execute to get back to the same state. But that is not avoidable, so the point is to mitigate the inconvenience.

我发现笔记本界面最方便用于开发扩展,因为它提供了最简单的获取方式回到同一州:

I find the notebook interface the most convenient for developing extensions, because it provides the easiest way to get back to the same state:


  1. 重建扩展名

  2. 重启内核

  3. 全部运行以重新执行笔记本

并且您将返回与新版本相同的状态延期。里程可能会有所不同,具体取决于您的互动作品重新投入的成本,但它对我有利。

and you are back to the same state with the new version of the extension. Mileage may vary, depending on how costly your interactive work is to re-run, but it has served me well.

这篇关于从IPython重新加载Python扩展模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆