wiki:DebugOTScript

How to debug an OpenTURNS Python script

Abstract

In this page, we present a method to debug a Python script which performs an uncertainty study with OpenTURNS. More precisely, we analyse how to use Spyder and the Python Debugger PDB on a sample OpenTURNS script.

Introduction

In practice, a debugger might be useful when we create or update a Python script which uses OpenTURNS. It may be because the script contains a bug that prevents from getting the required results. This may also happen when we have to create a complex script, for which we want to check that some intermediate calculation is correctly performed.

Whatever the reason, using a debugger is extremely useful, and allows to get the results more easily, and much faster. Fortunately, most serious Python environments come with a debugger, more or less integrated to the environment itself. Although the Spyder environment for Python does not come with an integrated graphical debugger, its Python debugger, called PDB, is both simple and efficient in many cases.

In this page, we presents how to use the PDB Python debugger which is shipped with Spyder to debug a simple OpenTURNS Python script.

Installing OT+Spyder

The first step is obviously to install OpenTURNS, and then to install Spyder. This step is presented in the page:

http://trac.openturns.org/wiki/IDEforOT

A sample script

In this tutorial, we are going to use the Axial stressed beam example which is available at:

http://trac.openturns.org/wiki/ExampleAxialStressedBeam#Script

We can open this file in Spyder, and get the following result.

The axial stressed beam Python script in Spyder.

A simple debug session

The simplest way to debug a script is to select a line and to execute it into the IPython interpreter. This can be done from the menu "Execute>Execute the line or the block of lines" or from the F9 key.

In the following figure, we execute the import of the openturns Python module.

Executing a line or a block of lines in Spyder.

But the most powerful debugging features come from PDB, the Python debugger [2]. This debugger is a standard Python module which can be accessed in Spyder through a dedicated console, with specific commands.

The Spyder editor integrates the Python debugger PDB in several ways [1]:

  • simple breakpoints and conditonal breakpoints may be defined in the Editor,
  • the current frame (debugging step) is highlighted in the Editor,
  • at each breakpoint, globals may be accessed through the Variable Explorer.

In order to launch a debug debug session, we use the menu "Execute>Debug" or the Ctrl+F9 key, as in the following figure.

The console prints the current line, just after the "->" prompt. Spyder also highlights the current line, before it is executed.

The console is now ready to execute our debug commands. Perhaps the most common command is "next", which executes the current (highlighted) line.

> c:\users\myname\downloads\axialstressedbeam.py(2)<module>()
-> from openturns import *
(Pdb) next

This produces the following figure.

The "next" command in pdb.

Now, to execute the next line, we can use either the "next" command, the "n" command (abbreviation from "next"), or, more simply, just type the ENTER key.

The most common PDB commands are the following.

  • "next (n)" : execute the current line
  • "<ENTER>" : same as "next"
  • "continue (c)" : execute until breakpoint
  • "where (w)" : print stack trace
  • "step (s)" : execute current line, stop inside a called function
  • "quit (q)" : quit
  • "list (l)" : see where you are
  • "return (r)" : continuing but just to the end of the current subroutine
  • "up (u)" : move one level up
  • "break [[filename:]lineno | function[, condition]] (b)" : add a breakpoint

Using breakpoints, exploring variables

Spyder allows to set breakpoints in the Python script. To do this, we just double click on the left of the line when we want to set the breakpoint.

A breakpoint in Spyder.

Then we use the "continue" statement to execute the script until the breakpoint is reached.

> c:\users\myname\downloads\axialstressedbeam.py(2)<module>()
-> from openturns import *
(Pdb) continue
Welcome to OpenTURNS version 1.0
x= [300,75000]
G(x)= [61.2676]
> c:\users\myname\downloads\axialstressedbeam.py(48)<module>()
-> sigma[1] = 5000.0
(Pdb) 

In the previous case, we have set a breakpoint just before the "sigma" variable is set. In order to see the value of the variable we just have to type the name of the variable in the console, and its content is printed.

(Pdb) sigma
class=NumericalPoint name=Unnamed dimension=2 values=[30,5000]

Seeing the value of a variable in the PDB console.

Conclusion

More details on Debugging with Spyder can be found at:

http://pythonconquerstheuniverse.wordpress.com/2009/09/10/debugging-in-python/

References

(1) "Spyder 2.1 documentation, Debugging", http://packages.python.org/spyder/debugging.html

(2) "pdb — The Python Debugger", http://docs.python.org/2/library/pdb.html

Last modified 4 years ago Last modified on 06/27/13 15:28:44

Attachments (8)

Download all attachments as: .zip