How can I reload a class definition in ipython

since I usually use ipython to play around with code under development, I come across the situation where I change the code of a class after I import it to ipython. I want to reload the new class definition without restarting ipython. The problem is that the reload function in ipython only works with modules.

I searched the solution to this problem and found a previous thread: how to reload a Class in python shell?

However, I followed the approaches there but it didn't work. I think the reason why it works in that thread is that the class name happened to be the same with the module name, so it can be found in sys.modules and thus can be deleted. When the class name is different from the module name, that approach cannot work.

I am wondering if there is solution to this problem. Thanks!


Yes, there's an IPython extension called 'autoreload' for exactly this sort of thing. Here's the documentation on how to use it.


You can also reload individual module

For example:

#reload every time  
import my_module
reload(my_module)

#reload a module recursively      
import deep_reload_my_module
dreload(deep_reload_my_module)
链接地址: http://www.djcxy.com/p/62826.html

上一篇: Android:无法为SharedPreferences文件创建目录

下一篇: 我如何重新加载ipython中的类定义