Following my post on IDEs for scientific Python, several commenters mentioned using some combination of their favorite text editor and ipython rather than a dedicated IDE. ipython is an interactive Python interpreter that offers a much enhanced user experience over the regular Python interpreter: with features such as:

- Integration with matplotlib
- Autocompletion
- Syntax highlighting at the command line
- Numerous magic commands like run, whos, debug, reset, etc.
- An interactive Mathematica/Maple-like notebook mode that runs in the browser

Here I cover what you need to know to install and use ipython and ipython notebook comfortably if you’re transitioning from Matlab.

### Installation

I wanted to install the ipython Notebook on my Ubuntu 11.10 PC but the installation information I got on the ipython website was confusing/outdated. I can tell you this much: there’s no ipython-notebook package for Ubuntu in the main repositories. There is an ipython package but it appears to be an old version.

I had an old version of ipython installed already (the current one is 0.13 – I was using 0.10, as revealed by `ipython -Version`

(for older versions) or `ipython --version`

. apt-get refused to upgrade, as did easy_install – but pip did the trick, with this line:

`sudo pip install ipython[zmq,qtconsole,notebook,test] --upgrade`

### Getting started with ipython

Starting ipython is as simple as running `ipython`

at the terminal. The default mode gives you a regular ipython interpreter, but you will most likely want to use the `--pylab`

flag, which will give you support for matplotlib. It appears to run `from maplotlib.pylab import *`

and `import numpy as np`

as well.

ipython works much like the regular python interpreter with a few obvious enhancements:

- autocompletion via the Tab key
- Enhanced command history via the up and down keys
- Get help associated with a variable via
`varname?`

and`varname??`

- Execution of system commands via
`!syscommand`

as with Matlab - More verbose, color-coded exceptions

In addition, ipython has a number of magic commands: these can be invoked via the `%thecommand`

, although if automagic is enabled (it is, by default), one can simply type the name of the command and ipython will recognize it as one of its special commands. The most useful are:

`%run file.py`

: runs a Python script within the ipython environment – equivalent to typing the name of a script file and pressing enter in the Matlab command window`%debug`

: runs the post-mortem (after an exception) debugger, pdb. Once pdb runs, you can type a single u and d to go up and down the call stack, and q to quit.`%prun myfun()`

: profile the myfun function (equivalent to`import cProfile; cProfile.run('myfun()')`

`%reset`

: clears variables from the workspace, similar to Matlab’s`clear`

`%whos`

: lists variables, similar to Matlab’s`whos`

`%edit myfile.py`

: edit, then execute the file in question. This uses the system editor, which appears on my system to be some incomprehensible text mode thingy (vi?). You may change the default editor by adding a line`export EDITOR=emacs`

(or gedit or whatever you prefer) in`~/.bashrc`

`%notebook -e filename.ipynb`

: export the ipython history as a Notebook`%magic`

: shows all the magic commands

In addition, there is an extension to ipython that will reload modules anytime they are changed, which is very useful when working on larger projects. To enable, use:

`%load_ext autoreload`

%autoreload 2

### Ipython notebook

The ipython Notebook interface can be started from the command line via `ipython notebook`

(requires ipython 0.12 or better). However, you probably want to open it with the flags `ipython notebook --pylab --inline`

for integration with matplotlib.

ipython will open up a web browser and display a list of notebooks, like so:

Create or open an existing notebook, and you will be treated with an interactive interface similar to that of Mathematica, Maple or Sage:

You can create a new cell by double clicking in a white area below the current cells. Code entered in cells is automatically syntax-highlighted. A cell may be executed by focusing on the cell, then pressing Ctrl+Enter. ipython will then print the output below, as though the text was typed in ipython:

Automagic commands will work as well. If the cell contains plot commands, the plots will be shown inline once executed:

This is a very cool feature.

As in Maple, it’s possible to create a cell which contains pure text to explain what is being done. Inside a cell, click on the dropdown menu which should have *code* currently selected, and change the cell type to raw text, Markdown or heading:

Markdown is a simple markup language which allows one to create headings, lists, and apply formatting intuitively. For instance:

###ipython notebook is like... - Matlab cell mode, but better - Sage - Maple - Mathematica > "I *love* it!" --me

Translates to:

Markdown cells support latex inside $$ tags, remarkably enough. Type:

Then click outside the cell and you get:

Notebooks can then be exported to regular Python as needed. It’s a really neat interface for writing self-documenting code and experimenting with new ideas – i.e. when you would use cell mode in Matlab, but better.

Since this post is aimed at Matlab users: another (potentially) useful tool for ipython is the Python/Matlab bridge, available here: http://arokem.github.io/python-matlab-bridge/

It allows calling out from the notebook to matlab (with the %matlab magic and %%matlab cell magic), passing variables back and forth between python and matlab and in-line display of matlab figures.

This can be installed from source (which can be downloaded in the above link), or using pip/easy_install.

Since this post is aimed at Matlab users: another (potentially) useful tool for ipython is the Python/Matlab bridge, available here: http://arokem.github.io/python-matlab-bridge/

It allows calling out from the notebook to matlab (with the %matlab magic and %%matlab cell magic), passing variables back and forth between python and matlab and in-line display of matlab figures.

This can be installed from source (which can be downloaded in the above link), or using pip/easy_install.