[IPython-user] ipython / namespace qn

belinda thom bthom at cs.hmc.edu
Fri Jan 5 02:20:16 CST 2007


As I continue to learn python via ipython, I'm widdling my questions  
down to the bare, crucial ones. I suspect some of my remaining  
confusion has more to do w/python conventions than ipython, but here  
goes anyway.

I'm still confused about how ipython and python interact wrt namespaces.

I understand that %who only displays variables defined after ipython  
config files have started up. Personally, I'd like to see a special  
switch to %who that can temporarily override that "feature" :-), for  
this behavior really confused me at first. (I'll try and remember to  
set up a ticket w/this request.)

I'm writing now b/c I'm confused about what run/reload/dreload  
actually do.

I've often had trouble figuring out how to reload files that are  
interdependent and being modified simultaneously.

My 1st big step was finding this quick tip:

"The %run magic command allows you to run any python script and load  
all of its data directly into the interactive namespace. Since the  
file is re-read from disk each time, changes you make to it are  
reflected immediately (in contrast to the behavior of import). I  
rarely use import for code I am testing, relying on %run instead."

I might not use import either if I knew how to "reset" all the  
dependent modules that have changed that the run code depends on. On  
occasion, I simply give up and restart py-shell. (I found myself  
doing that so much that I tried to write a little lisp function to do  
the work for me, but alas python's output got redirected to la-la  
land when I used this function.)

The manual tells me:

"The %dreload command does a recursive reload of a module: changes  
made to the module since you imported will actually be available  
without having to exit."

But who sees these reloads? The interactive namespace?

If I have module foo, which imports bar and baz, and supposing I've  
modified all three files, should I dreload foo and then run it?

How does dreload and run interact? And, supposing foo had "import baz  
as B" and "from bar import *", will dreload update what foo sees  
regarding bar and baz?
Here's what the manual has to say about run:

"%run: Run the named file inside IPython as a program.

%run [-n -i -t [-N<N>] -d [-b<N>] -p [profile options]] file [args]

Parameters after the filename are passed as command-line arguments to  
the program (put in sys.argv). Then, control returns to IPython's  

This is similar to running at a system prompt:
$ python file args
but with the advantage of giving you IPython's tracebacks, and of  
loading all variables into your interactive namespace for further use  
(unless -p is used, see below)."

Thanks in advance for taking the time to consider this long, layered  


More information about the IPython-user mailing list