IPython – an interactive shell
Scientists and engineers are used to experiment. Scientists created IPython with experimentation in mind. Many view the interactive environment that IPython provides as a direct answer to MATLAB, Mathematica, and Maple. You can find more information, including installation instructions, at http://ipython.org/.
IPython is free, open source, and available for Linux, UNIX, Mac OS X, and Windows. The IPython authors only request that you cite IPython in any scientific work that uses IPython. The following is a list of the basic IPython features:
- Tab completion
- History mechanism
- Inline editing
- Ability to call external Python scripts with %run
- Access to system commands
- Pylab switch
- Access to Python debugger and profiler
The Pylab switch imports all the SciPy, NumPy, and matplotlib packages. Without this switch, we will have to import every package we need ourselves.
All we need to do is enter the following instruction on the command line:
The quit()
command or Ctrl + D quits the IPython shell. We may want to be able to go back to our experiments. In IPython, it is easy to save a session for later:
Let's say we have the vector addition program that we made in the current directory. Run the script as follows:
As you probably remember, 1000
specifies the number of elements in a vector. The -d
switch of %run
starts an ipdb
debugger with c
the script is started. n
steps through the code. Typing quit
at the ipdb
prompt exits the debugger:
Tip
Enter c at the ipdb>
prompt to start your script.
We can also profile our script by passing the -p
option to %run:
This gives us a bit more insight in to the workings of our program. In addition, we can now identify performance bottlenecks. The %hist
command shows the commands history:
I hope you agree that IPython is a really useful tool!