Monday, December 12, 2011

Debugging in Python and OpenERP


Python debugger
Python provides a library for debugging called pdb.To use it, import the pdb module and add pdb.set_trace() at the line you want to start debugging.
When execution reaches that point, the console will prompt you for interactive debugging commands.
The most important are:
  • (n)ext, to step over to the next command.
  • (l)ist, to display the code around your current position.
  • (p)rint, to look into variables or test expressions.
  • (pp)rint, to pretty print expressions or variables. Useful for lists and dictionaries.
  • (c)ontinue running the rest of the code
You can also step in and out of subroutines:
  • (s)tep into the subroutines, used instead of (n)
  • (r)eturn, continues until the end of the current subroutine
Try it out using this script:



For more details on pdb  visit http://docs.python.org/library/pdb.html.

Debugging using Spyder IDE
Spyder allows you to debug your programs without having to add the pdb module to your code, using the Run » Debug menu command. You can set breakpoints in the editor windows and follow the next statement to be executed, highlighted in sync with the pdb console. The variable explorer window also helps you to inspect variables values at run time.

However, the execution control is done through console commands. Fortunately Spyder  integrates with winpdb. If you install this,  you will be able to control the step-by-step debug execution through a graphical window.


Debugging the OpenERP server
The simplest way to debug the OpenERP server is using it's native debug mode. To activate it you need to add the --debug option when starting the server. With this, it will go into (pdb) command mode when an exception is found. This is why it's important to be familiar with the pdb commands.

You can also import pdb and add pdb.set_trace() command to the specific module you want to examine. The set_trace() will open a (pdb) command line in the console, even is the server is not in debug mode, and you can examine the variables in memory (with print) and step into the next commands.


1 comment: