7

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!

Community
  • 1
  • 1
Wen
  • 83
  • 1
  • 5

2 Answers2

6

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

Thomas K
  • 39,200
  • 7
  • 84
  • 86
  • 1
    Does the autoreload only works for the class objects created after the autoreload is invoked? I had an class object that the definition had been changed. I tried the autoreload but it didn't work out for that class object. But when I created another class object after invoking autoreload, the new class object can be updated with the code that is consistently changing. – Wen Aug 23 '12 at 23:29
  • I'm not sure of the details, but it may well only work for things created after autoreload is enabled. – Thomas K Sep 03 '12 at 19:51
-1

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)
ChromeHearts
  • 1,693
  • 1
  • 17
  • 27