[IPython-dev] %bg %run -i foo.py

R. Bernstein rocky at panix.com
Sat Nov 11 00:21:24 CST 2006


Seems like no takers on this, so I'll comment. Possibly you know this
already though.

I don't believe anyone knows how to force an "unimport" in a general
way, especially when a module contains some sort of dynamically
loadable module (such as a DLL or shared object). So yes, a "restart"
is sometimes desirable.

I realized this a while ago so in the context of a debugger (e.g
pydb). So there is a soft restart (called "run") which preserves state
like breakpoints and debugger settings and any variables set. And
there is a "hard" restart (called "restart") which in fact does an
exec using the parameters it saved for the command-line invocation. In
the latter, there is no attempt to save debugger state. In theory, one
could write out breakpoints and other state to a file and read that
back in after restarting. (Maybe one day I'll add two commands to
save/restore debugger state command.)

But in the context of a python shell such as ipython, what would such
a "restart" mean? Pretty simple and straightforward to add would be a
"restart" which does the same thing -- just exec itself using whatever
command-line parameters were given before. I suppose the saved history
would give *some* access to previous commands, even if objects
themselves would be wiped clean. 

Given this, would that be helpful? I don't know. And if not, how would
one mark what to keep and what not to keep? Clearly not everything
should or could be kept: some objects might be the result of
instantiating various classes which did imports. More ambitious would
again be to add two commands to save and restore ipython state for
some definition of state, e.g. the value of "%pdb" (on/off) or the
current working directory.



Gael Varoquaux writes:
 > It seems this doesn't work. I am missing an obvious way to do this.
 > Elsewhere this would definitely be a nice feature to add.
 > 
 > Another nice magic to add would be a way to reload a module in a brutal
 > way. Let me explain. I have all the functions that I use often for
 > interactive work in a few modules. Every once so ever I change one of
 > them. Of course as I am a bad boy, I have imported this module doing a
 > "from foo import *", so I cannot reload it using "reload(foo)". The best
 > way I have found to reload it is something like
 > 
 > import foo
 > %run -i foo.__file__ 
 > # Well, this doesn't work, you have to copy and paste foo.__file__ here,
 > # as %run cannot take python variables as arguments.
 > 
 > I don't know whether I am clear on that, and I don't know if I am the
 > only one who needs such a magic but I for one would love to see this in
 > ipython.
 > 
 > Cheers,
 > 
 > Gaël
 > _______________________________________________
 > IPython-dev mailing list
 > IPython-dev at scipy.org
 > http://projects.scipy.org/mailman/listinfo/ipython-dev
 > 


More information about the IPython-dev mailing list