This course will become read-only in the near future. Tell us at community.p2pu.org if that is a problem.

Using IPython and IPython Notebook


Python is an interpreted language and comes with its own interpreter. Python can be used interactively inside the Python shell, and this is considered one of Python's strengths since it encourages "exploratory computing" that lets the programmer try out simple steps and algorithms before attempting to write functions and modules.

IPython is an enhanced shell for Python that is rich in features and makes life easy for those who use Python for scientific computing. starting IPython with the -pylab switch includes NumPy and SciPy in the standard namespace so that it is not necessary to import these modules before using the functions in these modules (This is convenient for interactive use, always include these modules in your scripts because they will be interpreted and executed by Python and not by IPython).

In addition to automatically importing NumPy and SciPy, IPython offers other features that are useful when using Python interactively:

  1. Inline help is very powerful. Auto completion by pressing the Tab key (similar to the feature in bash shell in Linux), ending a command with the question mark and pressing enter displays help about the specific command
  2. Magic functions that give let you do things such as run a Python script
  3. Access to filesystem
  4. IPython Notebooks are similar to Sage notebooks. It is a web based user interface for authoring and running notebook documents. Notebooks cancontain documentation (written in markdown markup syntax with embeddable LaTeX mathematical equations) as well as Python code. Python code is live, in the sense that it can be altered and executed within the browser.

First install Python and then install IPython. If you intend to do scientific computing, you will also need NumPy, SciPy and matplotlib. To use the IPython Notebook feature, you will need some additional modules, namely, ZeroMQ, Tornado. Look out for error messages when you start IPython in notebook mode and install the missing modules. To start IPython in the notebook mode, open up the terminal (Linux) and type the following command in Linux:

$ipython notebook --pylab inline

or, on MS Windows open the DOS Prompt by going to the WIndows Start > Run and entering cmd.exe and choosing OK. This will open the DOS Prompt and there type the followingcommand:

>ipython notebook --pylab inline

Note that, for the above command to work on MS Windows, IPython must be on your PATH environment variable. It is best to first change to the directory where you intend to store your notebook files and then issue the above command.

If everything is as it should be, you will see your default Web Browser open and display the interface to create, modify, save notebooks.

Notebook files have the extension .ipynb and are JSON text files that are interpreted by IPython when running in the notebook mode and output is seen in your browser.

To stop IPython when running in the notebook mode, go to the terminal or DOS Prompt and press Ctrl+C and close the browser window. Before this step, save any notebook files that have been modified and close the browser or browser tabs containing notebooks.

In fact, IPython HTML notebook mode can be invoked with the command:

ipython notebook

But then, any Python code that you write in the notebooks must explicitly import NumPy, SciPy and matplotlib as required. Further, graphs will appear in a separate window that pops up and not inside the notebook document.

To know more about IPython Notebooks, visit the IPython web page. You will also find a number of videos on YouTube.

In addition to writing your own IPython notebooks, you can also open, read, run and modify notebooks written by others. Since I intend to write and distribute IPython notebooks in this course, I thought it appropriate to discuss this topic right at the start.

Task Discussion