[IPython-user] ipython as zope interactive console

Fernando Perez Fernando.Perez at colorado.edu
Tue Oct 5 15:22:13 CDT 2004

Hi Jeff,

Jeff Kowalczyk schrieb:
> I wanted to start a thread to see what ideas are out there for configuring
> ipython as the interactive python console when running 'zopectl debug'.
> I read Fernando's forward of Jean Jordaan's tip on starting ipython within
> the normal python environment. I wanted something direct, so I made a
> slight alteration to the zope 2.7.2 source code:


> I'm sure the email client will mangle that; but the idea was simply to
> substitute the ipython binary. It seems to work perfectly. 

This seems like a slightly heavy-handed approach, and I hope we can find a 
solution which does not require modifying Zope.  In general, I've tried to 
make ipython _very_ easy to embed in third-party programs, and if there are 
limitations still which prevent this for Zope, I'd rather improve IPython's 
mechanisms than ask the Zope developers to accomodate ipython's shortcomings.

> - What would be the best cross-platform way to find the correct ipython
> binary? If a ZConfig directive is available, this point is moot.

There is no 'ipython binary'.  The executable ipython script is just this:

import IPython

But note that this is what is designed to start a fully standalone ipython. 
Here's a tiny snippet of an example I recently gave to someone else for 
embedding ipython as the interactive shell of a larger program:

planck[~/test]> cat sanner.py
#!/usr/bin/env python

from IPython.Shell import IPShell

# Code begins
import sys

# Create a global var, which the ipython shell should know about
pmv='python molecule viewer'

shell = IPShell(argv=['-colors','nocolor'],user_ns=globals())
shell.mainloop(banner='This is your ipython!\nTry "print pmv"')

Note that this requires CVS ipython to work, because I had to fix a few bugs 
which were uncovered by his request.  But it illustrates starting an ipython 
instance which knows about local variables (your Zope.app would play a similar 
role to the pmv string here).

The user_ns dictionary is the namespace which the resulting ipython will know 
about.  When we configure ipython for matplotlib usage, for example, we 
execute the following code:

         # Build a user namespace initialized with matplotlib/matlab features.
         user_ns = {'__name__':'__main__',
                    '__builtins__' : __builtin__ }
         exec 'import matplotlib' in user_ns
         exec 'import matplotlib.matlab as matlab' in user_ns
         exec 'from matplotlib.matlab import *' in user_ns

and then pass user_ns to ipython.

You might also want to have a look at the example-embed* scripts in the doc/ 
directory.  They show how to embed an ipython instance which can be opened in 
multiple places of a long-running code.  The difference between IPShellEmbed 
and IPShell is that the former is meant to be opened inside random user 
functions, so it plays stack tricks to extract the locals each time.  IPShell 
is meant to be run as a 'main' program via the mainloop() call.  They are 
otherwise very similar.

> - Effective user is a bit of a problem. IPython tries to write history,
> etc. into /root/.ipython/ when it is no longer that (root) user.

I imagine that if the ipython import calls are made once the UID has been 
switched, this should be OK.  But a bit more work may be needed here.

> - Lots of startup messages scroll by when starting a zeo client. It would
> be good to have an option of supressing them in the console without
> disturbing zope's config.

This will require a bit of input from zope experts, as I'm not exactly sure 
where this should go.  You certainly should NOT mess up sys.stdout of a 
running ipython instance, as that is quite likely to break things badly.  I'm 
not exactly sure what to do here.

> - Zope objects have *lots* of attributes! Any tips available for using the
> code-completion and attribute exploration with massive lists of attributes
> would probably help Zope newbies immensely.

Mmh.  I've run into the same problem with VTK objects, which also have monster 
attribute lists.  My approach is just to type obj.A<TAB>, obj.B<TAB>, etc: 
typing first a letter or two to narrow down the size of the completion list. 
Not ideal, I know...

> Thanks for any input. I'm thrilled to be using ipython against a live zope
> datbase, it really helps.

And hopefully we can find a 'ideal' way to set this up with minimal (or zero) 
disturbances of the zope codebase required.  It would be far easier for this 
setup to gain acceptance in the zope community if no core changes are needed, 
since I imagine zope tends to run in environments which are sensitive enough 
that messing with the core code is frowned upon.

Thanks for the interest!



More information about the IPython-user mailing list