[IPython-User] Embedding IPython troubles with shell profile / runlines

Stefan Frühwirth stefan.fruehwirth@uni-graz...
Wed Feb 2 08:30:43 CST 2011

Am 31.01.2011 20:53, schrieb Stefan Frühwirth:
> I have troubles using an embedded IPython instance. Here's how I used it:
> from IPython.Shell import IPShellEmbed
> ipshell = IPShellEmbed(['-p', 'sh'])
> First, when I call ipshell(), which should result in getting an embedded
> IPython prompt, a blank line is printed and the calling script exits.
> When I do the same thing without the -p sh argument - that is just
> IPShellEmbed() - it works.
> Second, I tried to pass a command in the form "var=!echo hi" to
> ipshell.IP.runlines. This works when running the script directly from
> the command line with "python script.py", but _not_ when called in a
> separate IPython instance via "run script.py". In the latter case I get
> a "SyntaxError: invalid syntax" exception. It also doesn't work when the
> code is included from another module (I have absolutely no idea why). I
> guess the error has something to do with the exclamation mark getting
> not escaped but I don't know why this is so only when running from "run"
> in IPython and another module.
> Is the runlines() method the right way to execute a string inside a
> python script?

I figured out that the troubles start if I have more than one ipy 
instance running in global scope. So it happens when I create two 
(parallel) instances, or parent/child instances or if I start an 
embedded Shell out of an interactive ipy session.

After some debugging I found out that a string with special "ipython" 
syntax isn't modified properly by the interpreter so that it doesn't 
become valid python code and then python complains when it tries to run 
such a code fragment.

More detailed the following happens: For some reason 
"ip.meta.re_prefilters" isn't populated. I guess this happens usually in 
Extensions/ext_rescapture.py , but I'm not sure whether this is the only 
place. This list defines the hooks (filters) which are necessary to 
escape magic and syscmd strings ($var = _ip.magic($expr)).

A quick hack is to do something like this after creating an IPShellEmbedded:

import ext_rescapture
ext_rescapture = reload (ext_rescapture)

That works, but I'm afraid that it has some bad side-effects :-/

Maybe a member of the dev team knows how to resolve this?

Another thing I noticed: When I embed ipy in a running ipy console, the 
exit() command does nothing. Ctrl+D works. I don't know why.


More information about the IPython-User mailing list