Python Programming Environments: iPython

Python programming and direct interaction with Python can be done in a terminal window. But, as one immediately realizes, the interactively entered commands are lost and this, in particular, makes writing and debugging a program tedious. Not surprisingly, like other modern programming languages, Python has several environments that make program development more convenient and program debugging faster.

The first chapters of Learning Python describe a number of these environments, including one called IDLE, which comes with most distributions of Python.

I recommend iPython. Here is a tutorial; here its online manual; and here are several videos. Finally, iPython when it starts up presents you with an option to read an introduction.

Also, when starting it up, use ipython -pylab. This establishes the correct environment so that the graphics display windows properly shutdown and return control to the iPython interpretter.

iPython is really a shell for Python. Compared to GUI-based program development environments, using iPython provides a fairly bare-bones environment, but it is a reasonable place to start.

iPython lets you use your favorite text editor for editing program files. You can configure this however you like. For most, there won't be much to configure.

For example, vi is the default on Unix/Linux/Mac. (If you're new to vi, take a look at the vi tutorials.)

If you're an emacs fan, see iPython's setup and other paragraphs on emacs in the iPython tutorial.

Ready!

Start-up ipython in a terminal window:

101}ipython                                                             
Python 2.7.3 (default, Apr 19 2012, 00:55:09) 
Type "copyright", "credits" or "license" for more information.

IPython 0.12 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]:

To edit a program file, we use one of iPython's magic commands (which begin with a '%'):

%edit
If you want to see the online documentation for its options, type
%edit?
at the iPython prompt. (You hit the spacebar to page through the documentation. Hit 'q' to exit.)

The edit command, and many of iPython's magic commands, can be abbreviated. %edit becomes simply ed. So to edit the file test.py, you enter

ed test.py
Here's what I put in test.py:
print 'Hello world'
print 2**100
Now exit the editor. You then see on the terminal
Hello world
1267650600228229401496703205376
That is, %edit automatically runs the code in the file, when you exit your editor.

You can suppress this, by using the -x option:

ed -x test.py

You can run a particular file, using

run test.py
where test.py can be any Python program. Note that run is an abbreviation of the iPython %run magic command.

That's basically it for writing and running Python programs within iPython.

Naturally, there are many, many more convenience features in iPython.

To explore, see the resources at the iPython documentation page.


chaos@ucdavis.edu Last updated 27 September 2012