python 重新加载模块_从IPython重新加载Python扩展模块

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)

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 extensions cannot be reloaded without restarting the process (see this official Python bug for more info).

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:

rebuild the extension

restart kernel

Run All to re-execute the notebook

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值