Tested on:Raspbian Jessie; Python3
0

A Primer on Debugging and Developing with the IPython IDE and IPDB

The IPython IDE is an interactive development and debugging tool and a great help in Python software development. It’s also the core of the wonderful Jupyter IDE.
Debugging short pieces of code can be done by printing variables, calling functions with edge parameters and so on. If the code grows, a more efficient approach is necessary. IPython helps massively with introspecting code, stepwise program execution, runtime analysis and so on. A step further would be automatic software tests.

IPython installation
For Python3 and a Raspberry Pi with Jessie, just enter in the terminal

Start IPython
Start the IPython IDE from a terminal with

Basic IPython usage
The IPython IDE looks similar to the normal Python IDE, but distinguishes input and output lines clearly.

IPython IDE

IPython IDE

In IPython, the all-beloved tab completion works. Test this by defining a string, then pressing Tabulator after the ., which shows all attributes and methods (except the ones starting with _):
IPython tab completion

IPython tab completion

Enter %quickref and %magic to display the most important IPython features. Putting a ? before or after any object will output general info and the object docstring if defined:
IPython inline help

IPython inline help

?? instead will also display the source. Together with *, ? can search object attributes/methods:
IPython object search

IPython object search

System calls can be performed with the ! and their output can be read into a variable:
IPython system commands

IPython system commands

Variables can be passed to system calls with the $:
System command parameters

System command parameters

Earlier input and output cells can be accessed with _iX for inputs and _X for outputs with X being the cell number:
 Access earlier cells

Access earlier cells

Debugging with ipdb
So far, you’ve learned some basic IPython usage. Now, this broken piece of code is examined with the IPython debugger ipdb:

Download the demo script with

Then, enable automatic debugging with

and run the script with

Another option for starting ipdb is from within the script by adding the two lines

above the line

in buggy_script.py, then running the script from the terminal with

Both ways activate the debugger and show the stack trace:

IPython stack trace

IPython stack trace

The stack trace shows the path of the exception from the module that detected the error down to the specific piece of code that caused it.
ipdb> is the pdb command line. With ? only, you can see the available pdb commands. You can execute the program stepwise with s, show the function arguments with a, output variables, call the functions with edge case parameters ect..
IPython debugger

IPython debugger

Aha! calculate received 1 for a and 0 for b as parameters, which caused rightfully an division by zero error.

Links
Jupyter Installation: http://www.knight-of-pi.org/installing-jupyter-on-a-raspberry-pi-for-notebooks-debugging-and-data-analysis/
Test-driven project setup: http://www.knight-of-pi.org/python-project-setup-test-driven-development/
IPython Website: https://ipython.org/

Knight of Pi

Johannes Bergs aka Knight of Pi. Diploma in Bioinformatics, some Webdesign and Python coding then. Living in the beautiful city of Vienna.

Comment

Time limit is exhausted. Please reload CAPTCHA.