4 min read

IPython is an alternative Python prompt, adding a lot of new features such as autocompletion, easy history calls, code coloration, and much more. It is also the project that started developing the IPython notebooks, now known as Jupyter notebooks.

IPython implements a lot of helpers, allowing you to get object information much more easily than in the standard Python prompt. As stated in the IPython documentation, the four most helpful commands in the IPython prompt are ?, %quickref, help, and object?, giving very general information about IPython or a particular object. If you read through %quickref, you will see it is very complete, but also quite dense. We will try and get out some of the most helpful commands when in an interactive coding session.

One of the most useful functions added by IPython, is the %pdb function. By typing this in your prompt, any exception raised but uncaught will put you in PDB, the Python debugger. For example, we will have the following:

In [1]: %pdb
Automatic pdb calling has been turned ON

In [2]: 1/0
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
<IPython-input-2-05c9758a9c21> in <module>()
----> 1 1/0

ZeroDivisionError: division by zero
> <IPython-input-2-05c9758a9c21>(1)<module>()
----> 1 1/0

ipdb>

On In[1], we enable pdb mode. We then try and compute 1/0, obviously raising an error. The prompt then changed, putting us in ipdb, being the IPython debugger (which has all the pdb functionalities, and more, such as syntax coloration). From there we can start using the debugger by launching it from the shell. As an alternative, you can also call %debug afterwards. You will find more detail in the documentation.

If you’ve ever done interactive work with the prompt, you know it is good for one liners, but as soon as you increase the length of your tested code (which happens almost every time), it starts to get difficult, because the prompt was not made to be a text editor. In order to solve this problem, you can use the %edit command. You will be put in the editor defined in your $EDITOR environment variable, or a default text editor. You can re-edit a previously edited code part using:

%edit _

Or for a precise history item:

Out[5]: 'print("Hello, World!")n'
...
In[12]: %edit _5

This will edit the print statement created during the edit on Out[5]. Using this function, trying functions or classes becomes much more easy (remember when you had to go back 20 lines up by keeping the left arrow down, and it took you about 3 years?). See the documentation for more information.

Easy repetition :

During a session, you will often find yourself repeating lines of code. Even though using the up arrow is very useful, if you have to repeat 20 lines of code each time, it gets quite time-consuming. To avoid doing this, you can use the %macro command. It allows you to repeat a series of commands from your history. For example, if you typed the commands:

In [1]: a = 1
In [2]: b = 2
In [3]: c = 3
In [4]: d = 4
In [5]: e = 5

You can repeat some of these commands by defining a macro:

In[6]: %macro my_macro 1-3 5

which will repeat the commands 1 to 3 and 5, skipping 4. In fact, you can define a macro with the commands in any order you want. You can for example write:

%macro my_macro 5 2 3 1 4

executing the commands in the macro in the order you defined. You can also make a macro from a script, just by passing the filename:

%macro my_macro filename.py

By calling my_macro, you will execute every command loaded from the file. For more details, see the documentation.

Single file loading :

You can also load any Python file in your environment with the %load command:

%load filename.py

You will load every object defined in the file and make them available for use. You can also choose to load only certain symbols by using:

%load filename.py -s symbols

This is useful if you want a single function from a file without loading everything.

Increase your productivity with editing tools :

External editing and macros are very useful tools, but they are session-specific. However, you can save them for later, just by using the %save command.

%save macro.py my_macro

This will save the macro my_macro in the file macro.py in the current working directory. If you want to save an edited file, from the history element 53, you can do:

%save edited.py _53

Which will create the file edited.py in the current directory.

Using %edit, %macro, and %load with the %save command can enable you to increase your productivity a lot. If you want to generate a testing environment with predefined objects, create it once with %edit, then save it. The next time you want to use this environment again, you can either %load it, or put it in a %macro, if you plan to reuse it quite often.

Increase your productivity even further by finding out how to customize IPython in our next article.

About the author

Marin Gilles is a PhD student in Physics, in Dijon, France. A large part of his work is dedicated to physical simulations for which he developed his own simulation framework using Python, and contributed to open-source libraries such as Matplotlib or IPython.

LEAVE A REPLY

Please enter your comment!
Please enter your name here