Accessing an IPython powered shell
IPython, a Python shell on steroids, provides an interactive environment for programmers, enriching and easing their experience during the development and debugging stages of the project. Some of its highlighted features are:
- Object introspection
- Code introspection
- Documentation introspection (
%pdoc
magic command) - Input history, persistent across sessions
- Tab auto-completion
Note
To learn more about IPython visit the project website at: http://ipython.scipy.org/moin.
These and more of its features will be quite useful to get used to Plone's code and to get to know better its API and documentation. IPython's particular mode to write doctests will also be a great help when working with them (refer to Creating doctests with IPython in Chapter 4 for more details about this).
How to do it…
To take full advantage of IPython inside Zope, we will create a new executable file in the bin
directory: ipzope
. To do so, we will add some lines in the buildout.cfg
file:
- Include a new
[ipzope]
part at the bottom of the file:[ipzope] # an IPython Shell for interactive use with zope running. # you also need to put # https://svn.plone.org/svn/collective/dotipython/trunk/ # ipy_profile_zope.py # to your $HOME/.ipython directory for the following to work. recipe = zc.recipe.egg eggs = ipython ${instance:eggs} initialization = import sys, os os.environ["SOFTWARE_HOME"] = "${zope2:location}/lib/python" os.environ["INSTANCE_HOME"] = "${instance:location}" sys.argv[1:1] = "-p zope".split() extra-paths = ${zope2:location}/lib/python scripts = ipython=ipzope
- Add the just created
ipzope
to theparts
definition at the top of the file:parts = zope2 productdistros instance zopepy plonesite ipzope
- Install Python required modules (just for Windows): On Windows, you will need the PyReadline and ctypes packages. The easiest way to install PyReadline is to use the binary installer available at https://launchpad.net/pyreadline/+download. Download
ctypes
from http://python.net/crew/theller/ctypes and install it. - Build your instance. Finally, build again the Zope instance to let it know that IPython is available:
./bin/buildout
How it works…
Once built the new improved instance, we can run it in a special mode (when run in this mode, Zope won't listen to any port but to the direct console input) to get access to an IPython-powered shell:
./bin/ipzope
The first time you run ipzope
, a new IPython profile directory will be created in your home folder. You'll be notified of this as shown in the following screenshot:
In Linux, you can find this new folder at $HOME/.ipython
. In Windows, it will be created in %userprofile%\_ipython
(notice the underscore instead of a dot).
There's more…
To get used to ipzope
and IPython facilities, we will launch ipzope
and play around with our Plone site by setting some properties in its error_log
tool:
- Open an IPython shell by running
./bin/ipzope
. - Write
portal.error_log.get
and then press the Tab key to get the list oferror_log
's available methods beginning withget
. - Check the results of
getProperties
by runningportal.error_log.getProperties()
. - After this, you should see that
setProperties
method is the one to use. But what is the syntax? Try%pdoc
to get its docstring:%pdoc portal.error_log.setProperties
. - Was this helpful? Not really, so let's try with a different one. Try
%psource
to get the source code:%psource portal.error_log.setProperties
. - Great! So we now know how to pass the parameters to the previously mentioned
setProperties
method. Go ahead and clean theignored_exceptions
property by runningportal.error_log.setProperties(20, True, ())
. - Done. Sure? Let's check:
portal.error_log.getProperties()
. - Don't forget to save the changes (that is, commit):
utils.commit()
. - Although we are working in a standalone Zope instance (
ipzope
doesn't listen to any port), it is always worthwhile to synchronize our session with changes other people might have made while we were working by usingutils.sync()
.
Now, if you close the IPython session (by pressing Ctrl + D) and launch your Zope instance to open the Plone site in a browser, you'll see your changes at http://localhost:8080/plone/prefs_error_log_form
.
Note
More information about IPython can be found at: http://ipython.scipy.org/moin/.
See also
- Creating doctests with IPython