views:

41

answers:

2

Hi,

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.

A: 

If the object is a class or a function, you can use its __module__ attribute to determine which module to reload:

def reload_for(obj):
    module = reload(__import__(obj.__module__, fromlist=True))
    return getattr(module, obj.__name__)

MyClass = reload_for(MyClass)
jleedev
+2  A: 

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 normal reload() 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:

import ipy_autoreload
%autoreload 1
WoLpH
`dreload` still only loads modules.
jleedev
@jleedev: the `autoreload` command does exactly what he needs. But you are right, `dreload()` isn't sufficient either.
WoLpH
This seems to work! The only downside is that it seems to be reloading upon *every* carriage return (i.e. executed line statement in IPython). Otherwise, it's great!
reckoner