Simply put, IPython lets you do all sorts of really powerful stuff. It’s very popular amongst scientists and mathematicians, and has a lot of features that they seem to really appreciate. It also has a lot of features that are really useful to everyone else who uses Python.
IPython offers lots of simple hooks for you to customise and extend the way it works. Because it’s so easy to modify what IPython does, it can be quite difficult to define exactly what it is. When you first install it, you get an app that essentially boils down to an enhanced, Python, interactive interpreter. You can do all the stuff you can do in a regular, interactive session, and you can do quite a bit more.
IPython Magic Commands
The very first feature you’ll notice is the coloured prompt, a green
In :. The colour’s always cool, and it’s put to good use, but IPython’s not just a pretty stack trace. A more powerful enhancement to the standard Python session is the use of Magic Commands – Python functions that you can call using a shell-like syntax. You get a bunch built in, some really clever ones, and you can easily define your own. To use a magic command, you just type its name, then any arguments, separated by a space. For example:
magicname arg0 arg1
Some of the built-in magics are there to make the Python session operate more like a conventional shell. You have
cp and so on. If you like to use these commands, you can just type them in as you normally would:
If you want to use shell commands that you don’t have a magic set up for, you can just start the line with a bang and write any command that would normally work:
Editing the IPython Namespace
One of the most useful magics, I think, is
edit. If you want to write any moderately complex Python, you don’t want to do it at the command line. In IPython, rather than starting with a text editor, then running the file, as you normally would, the process is reversed: We start at the prompt and simply invoke an editor from within the session. You can use any editor you like for this, but Vim rules.
You can use edit by just entering ‘edit’ at the prompt. IPython will open your editor with an empty tempfile. When you save and exit the editor, any code you wrote is injected into the namespace.
For example, you could enter ‘edit’ at the prompt, then define a function named spam in your editor, then save the file and exit the editor, then call spam at the prompt, passing whatever arguments you like. What’s especially cool, if you later enter ‘edit spam’ at the prompt, IPython reopens whichever tempfile contains spam, so you can edit it some more.
There’s also ways to inject the contents of regular files into the same namespace. And, of course, you can save what you’ve built, using your editor’s save feature, or just writing stuff to files from the command line. There’s also a magic named store that you can use to make objects persist across sessions.
The ability to easily build a namespace interactively, developing it as you go, gives you much more freedom to explore and interact with the objects you’re creating. This approach is a natural fit for writing Python, which you’d normally develop rapidly and in very small increments.
Configuring the IPython Interpreter
IPython has a powerful configuration system, which is pure Python itself. You can use it to create and manage any number of configurations, customising each of IPython’s features, as well as specifying code to be automatically injected into new sessions, further enhancing your ability to fully customise the way your interpreter works.
Pretty much everything is configurable, and, if you know a little Python, it’s easy to do.
Developing your IPython Environment
Once you’re comfortable using IPython, you should check out other articles on Python Central that cover the main features in more detail, including defining your own magic commands and configuring sessions.
Over time, as you use IPython more, and add more to it, you develop a heavily customised, console IDE, conveniently wrapping everything else you’re doing in Python.
Grab a copy from ipython.org, and let us know where you end up going with it.
Also, for a more in-depth look and review of IPython, see the article review of IPython.