[IPython-user] help with embedding

David M. Kaplan dmk@ucsc....
Fri Jul 11 09:23:38 CDT 2008


Thanks for the response.  I will submit a bug report.  I have a few
comments/responses below.

On Fri, 2008-07-11 at 04:01 -0700, Fernando Perez wrote:
> > 2) The restriction to not being able to modify variables is a bit of a
> > pain.  In matlab, I often use keyboard to give information to my
> > scripts.  Is there a way around this?  Or is there another approach I
> > should be using?
> There's no way around it, it's a limitation imposed by python itself:
> the locals dict obtained from the stack is read-only.

It has been hard for me to figure out how python treats namespaces, but
if I understand correctly, the local namespace is read-only, while the
global is writable.  If this is true, I would naively think that the
following would enable you to modify ipython variables:


But this doesn't work at all, probably because I don't completely
understand how locals and globals work.  I imagine the answer is that
locals() is always read-only, no matter which namespace IPShellEmbed
tries to attach it to.

This relates to one potential solution I found for having a more
"keyboard" like experience (except for being able to modify variables),
that also didn't work:

def keyboard():
	IPShellEmbed(['-prompt_in1','Keyboard <\#> '])(local_ns=caller_locals,global_ns=caller_globals)

This would allow me to call keyboard and get a look at the namespaces of
the script or ipython that called keyboard, but I can't figure out how
to get "caller_locals" and "caller_globals".  Perhaps someone can give
me a hint....

> Personally, I do use the embedded shell a lot, but when I do so I call
> it in standalone scripts.  For quick and dirty inspection of the
> internal state of a script, I often just put
> 1/0
> and call %debug afterwards.  This is post-mortem only, but often tells
> me what I need to know.

This is a reasonable solution that should provide much of what I need.
But often times I like to repeatedly stop a script and see how things
are going.  In this case, generating an exception isn't what I want.
But IPShellEmbed should work in these cases, except for being able to
change things on the fly.

One other thing I have found - using IPShellEmbed freezes any pylab plot
windows that are open.  As often times I use this to look at those plots
before proceeding, this seems like a show stopper.  Is there a way to
stop the execution of the script without impeding plot window

Thanks again,

David M. Kaplan
Assistant Researcher
UCSC / Institute of Marine Sciences
Ocean Sciences
1156 High St.
SC, CA 95064

Phone: 831-459-4789
Fax: 831-459-4882

More information about the IPython-user mailing list