Contents
IPython is an enhanced Python shell that creates a comprehensive environment for interactive and exploratory computing. In this section we’ll learn the basic features of IPython and how it benefits interactive analysis.
Before going further, open a new terminal window and change to your main working directory. Then start IPython by typing ipython --pylab at the command prompt:
$:> ipython
The first time you start the ipython shell, a hidden directory .ipython will be created in your home directory (/home/user/ on unix or C:\\Documents and Settings\user on Windows, henceforth called IPYTHONDIR).
It can be useful to always execute a standard block of code when you start IPython. For example, if you always want to have access to the standard glob and os modules, you can write a small script only containing:
import glob
import os
And save it to IPYTHONDIR/pythonstartup.py for example. Then, add the following line to the IPYTHONDIR/ipythonrc file:
execfile IPYTHONDIR/pythonstartup.py
Next time you start the IPython shell, the two modules will be loaded.
To exit IPython, hit <CTLR+D> or type exit or quit in the shell.
TIP: a MATLAB style environment
There is an easy way to automatically load the basic numerical and graphical packages. When starting IPython in a terminal, simply add -- pylab to the command:
$:>ipython --pylab
Extra benefit: when you normally load pylab and a plot is shown to the screen with pl.show(), you have to close the plot if you want to continue working in the shell. With the --pylab option, plots are shown in a separate thread, allowing you to simultaneously edit data and view a plot:
In [1]: x = np.arange(0,10,0.2)
In [2]: y = np.sin(x)
In [3]: print x
[ 0. 0.2 0.4 0.6 0.8 1. 1.2 1.4 1.6 1.8 2. 2.2 2.4 2.6 2.8
3. 3.2 3.4 3.6 3.8 4. 4.2 4.4 4.6 4.8 5. 5.2 5.4 5.6 5.8
6. 6.2 6.4 6.6 6.8 7. 7.2 7.4 7.6 7.8 8. 8.2 8.4 8.6 8.8
9. 9.2 9.4 9.6 9.8]
In [4]: plot(x,y)
A select set of useful linux commands are available from the IPython prompt. These include ls, pwd, cd, and rm. Any shell command can be executed by preceding it with an exclamation point !.
IPython has a very useful tab completion feature that can be used both to complete file names and to inspect python objects. As an example do:
In [5]: ls ~/<TAB>
This will list everything in your home directory. You can continue this way searching through files or hit Return to complete the command.
So far we typed print x to look at the value of x. However, most of the time for interactive analysis it is faster and better to simply type x (or whatever the object name) followed by <Return>. This returns the “representation” of the object which is often a cleaner and more informative than the “string” version that gets returned with print. In many cases the “representation” of an object is the same as Python code to create that object.
Try:
In [6]: y = dict((x, 'value is %d' % x**2) for x in range(10))
In [7]: y
In [8]: print y
Suppose you are developing a module, say mymodule, and you want to test the function myfunction within that module. First, you need to create a file named mymodule.py and create the function:
def myfunction(argument1):
output = argument1 / argument2
return output
First, you need to import the module, and and then call the function. Of course it will raise a NameError, since argument2 is not defined:
In [9]: import mymodule
In [10]: mymodule.myfunction(5.)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
/home/user/workdir/<ipython-input-2-88c3fd4bd2ac> in <module>()
----> 1 mymodule.myfunction(5.)
/home/user/workdir/mymodule.py in myfunction(argument1)
1 def myfunction(argument1):
----> 2 output = argument1 / argument2
3 return output
NameError: global name 'argument2' is not defined
You need to edit the file and solve the bug. Either open your favorite editor in another terminal or type
In [11]: edit mymodule.py
Then, change myfunction so that the bug is solved:
def myfunction(argument1,argument2):
output = argument1 / argument2
return output
If you now run the above again:
In [12]: import mymodule
In [13]: mymodule.myfunction(5.)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
/home/user/workdir/<ipython-input-2-88c3fd4bd2ac> in <module>()
----> 1 mymodule.myfunction(5.)
/home/user/workdir/mymodule.py in myfunction(argument1)
1 def myfunction(argument1):
----> 2 output = argument1 / argument2
3 return output
NameError: global name 'argument2' is not defined
You see that you end with exactly the same error! The reason for this is that once a module is loaded, a new import statement does not reload it again. Before loading a module, the Python interpreter checks if it already exists in the local namespace. If it does, the interpreter will simply skip loading the module. This means that a module can only be loaded once. This is valid in general and is not a restriction of IPython. To force a reload, you can do:
In [14]: reload(mymodule)
In [15]: mymodule.myfunction(5.,10.)
Out[15]: 0.5
Warning
The command reload is not recursive! To force a reload of all modules and submodules, you need to import deepreload:
In [16]: from IPython.lib.deepreload import reload as dreload
In [17]: dreload(mymodule)
The run command allows you to run any python script and load all of its data directly into the interactive namespace. Since the file is re-read from disk each time, changes you make to it are reflected immediately (unlike imported modules, which have to be specifically reloaded). You can run existing scripts from the shell via:
run myscript.py
Suppose there is a bug in your script and you want to run it again (you can do edit myscript.py from the script, which will load your favorite editor). Simply do:
run myscript.py
This text is based on, or has elements taken from: