The python debugger in the WebIDE can be quite useful for many situations.  The debugger allows you to step through your python program in realtime.  This is in contrast to the visualizer, which runs the program fully, and then allows you to step through it.

When would you use the debugger?  One scenario would be if you're just not getting the right values returned out of your functions.  Or your variables aren't being set to the values you'd expect.  You could then debug it, set a breakpoint to where you think it's broken, and see exactly what the values are at that given moment in your program.  Also, you could use the GPIO pins to light various LED's.  As you step through your program, you'll see them turn on and off as you'd expect.

To use the debugger, open a python file, and click the "Debug" link in the toolbar:
Once you've clicked the link, the toolbar will change with a new debugging bar.  It should open within a couple of seconds.  The editor will still display, and you can make changes to your code while debugging.

Once the debugger is ready, you'll see the following:
There is quite a bit more to the debugger than just running a program, or even than using the visualizer.

The first think you'll notice is the toolbar (#1) has new buttons available:
  1. Save/Restart allows you to save your file, and restart the debugger for that file in one click.  You can initiate that by hitting control-s, or command-s (on OS X).  
  2. The Exit link will exit the debugger.  
  3. The Run link will continue execution of your program either to the end, or to the next breakpoint you have enabled.  If there is a breakpoint enabled (clicking in the gutter (#6)), it will stop there, and wait for your instruction.
  4. Step Over will step through your program line-by-line until it hits the end.  This option will skip any function calls.  So, if you have a function called foo(), it will just run the entire function, and move to the next line.
  5. Step In will step through your program as well, but it will jump into many of the functions, instead of skipping over them.
The editor is also displayed (#2).  You can edit your file at any time.  If you do change your file, you'll need to click the Save/Restart link in the toolbar in order to let the debugger pick up any of the changes you've made.

At the bottom two panels, you'll see the Debug Output (#3), and the Debug Variables (#4).  The Debug Output displays anything from stdout or stderr that your program would output (such as print statements).  The Debug Variables displays the live variables as they're being assigned.

The red line in the editor (#5) moves as you step through your program.  It is the line that will execute next, and will stay centered in the editor window as you step through your program.

At the far left of the editor (#6), there is a blank space to the left of the line numbers.  This is called the gutter.  If you click in the gutter when the debugger is in a "Ready" state, you can add breakpoints.  A breakpoint is useful for many situations, such as if you have a longer script, and there is a certain troubling section that is buggy.  Instead of slowly stepping through your program you would set the breakpoint (a red square will appear in the gutter), and then click "Run".  The script will execute to the breakpoint, and the red line will stop, and wait for you to continue stepping through it.

The last feature you'll want to be aware of is the debug status messages (#7).  These will show you what the debugger is doing.  For example, when it initially loads, or when you click "Save/Restart", it will be in an "Initializing..." state.  When it's ready for your input, it will be "Ready".  Some parts of your script will cause it to appear locked up, but it's really waiting for the server to return a response from your script (a long sleep() statement could cause this, for example).
In the screen shot above, you can see what the debugger looks like in the middle of running a program.  The debugger is currently one step after the breakpoint that was set on line #13.  You can see the 'print "Bonjour, World"' in the Debug Output, and the variables as they are in that given state of the program execution.

At this point in the debug cycle, you could edit your file, and Save/Restart.  You could continue stepping through the program, or you could Exit the debugger.

This guide was first published on Oct 05, 2012. It was last updated on Sep 04, 2012.

This page (Using the Debugger) was last updated on Jan 06, 2013.

Text editor powered by tinymce.