[IronPython] Embedding IPython Shell into IronPython

Dave Wald davew252 at tx.rr.com
Fri Feb 11 05:09:57 CET 2011

First off, Thanks to all the developers! 2.7b1 and b2 have been 
excellent so far, at least as far as my current needs go. The new 
subprocess module is great. Came just in time. Great work!


Your post caught my eye, as I also like good embeddable console apps. 
Hadn't used IPython before so I downloaded it and played with it. 
Installed the PyReadline library as recommended to get all the command 
line goodies. Nice.

OK, so then, of course, I had to try it in IronPython, and immediately 
hit the same issues you did, with almost the exact same traceback info, etc.

So, I fired up the excellent Tools for Visual Studio 2010 and created a 
little project to embed the shell and call it from my running 
IPyScratchPad app (with a WinForms dll to house the form) and traced 
thru it in the Debugger.

Long story short, I found the major issue (and a couple of minor ones) 
and got it to run.

Now, I'm pretty sure Readline has not been ported to IronPython yet, and 
you know it requires pywin32. So that is not going to run under IronPython.

But the rest of it seems to be working ok. No tab completion or 
colorization but...

Here's what I did (ultimately) to get it to run:

First of all, copied the whole IPython folder structure over under 
"C:\Program Files (x86)\IronPython 2.7\Lib\site-packages".

I created a new Scripts folder under "C:\Program Files (x86)\IronPython 
2.7", as IPython creates under Python 2.6.

Copied thefollowing two files into it:



Modified ipython.bat file to read:

cd "C:\Program Files (x86)\IronPython 2.7"

ipy.exe -X:Frames .\Scripts\ipython.py %*

  Double-clicking the bat file will now start up the IPython console 
running in IPy.exe.

  Notice the " -X:Frames " option specified. That is the major key to this.

Turns out, IronPython does not enable sys._getframe by default, which is 
required by IPython.

Note (from the Python doc):


Return a frame object from the call stack. If optional integer depth is 
given, return the frame object that many calls below the top of the 
stack. If that is deeper than the call stack, ValueError is raised. The 
default for depth is zero, returning the frame at the top of the call stack.

CPython implementation detail: This function should be used for internal 
and specialized purposes only. It is not guaranteed to exist in all 
implementations of Python.

Found a post on StackOverflow, with a reply by Jeff Hardy about this, 
which is what tipped me off. Thanks Jeff!


  OK, so far so good.

Now. Embedding...

  I used the simplest example in the IPython manual pdf, in section 
"3.2.4 Embedding IPython" on page 32-33.

I placed the following code into my app, in a button-click event handler.

def testIPyShell(self, sender, event):

from IPython.Shell import IPShellEmbed

args = ['-colors','NoColor','-noreadline','-classic','-x','Verbose']

ipshell = IPShellEmbed(args)


In the Project Properties page, I set "Command Line Arguments" to   
-X:Frames    and Interpreter Path to    C:\Program Files 
(x86)\IronPython 2.7\ipy.exe
(which is now 2.7b2) and check the "Debug Standard Library" box.

Hit F5, click the button and it works. The shell starts up. Main app 
blocks on it, tho. Hmmm. Well, may be a way around that too.

But it starts up and runs. Type "%magic" or "?" and it prints out all 
the doc, etc. Shell commands seem to work ok.

There is a minor issue in file iplib.py, line 646:

self.usage_min ="""\

where it continues a multi-line string. The trailing back irritates the 
VS2010 Debugger to where it gets out of sync with the code.

I just took it out. If you're not running the debugger, you probably 
won't ever notice it.

If you ARE running the VS2010 debugger, you will get all kinds of 
stoppages, anywhere anything is raised. And it does that a lot.

Just ignore them. Hit the continue button.

And that's about it. Pretty cool little console.

All that having been said, of course, I think there may be an even 
better option. Possibly.

Check this out: 

Curt Hagenlocher posted this a couple of weeks ago. "Sho" is an 
IronPython shell from MS Research Labs. Played with it some. It's pretty 
cool. Very geared towards math/science/engineering apps.

The doc says it can be "embedded", but from what I have read (haven't 
tried it yet), it looks like you can call into it and use it's code and 
libraries of course, but I don't think the console will pop up. Don't 
know for sure. Need to play with it and see. That's next.

(Also, downloaded SilverShell http://code.google.com/p/silvershell/ 
(written in IronPython and WPF) coupla weeks ago and got it running 
under 2.7 - it's nice, but... seems to have some issues. I think I like 
Sho better for simplicity and raw power. You might give 'em a try and 
see which works better for you.)

That's all for now. Have fun with IPython. Holler if you have any questions.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20110210/0147b711/attachment.html>

More information about the Ironpython-users mailing list