Debugging is a developer’s super power, so I’m keeping it quiet that I didn’t know about the python equivalent of ruby’s byebug.

In python you can launch the debugger by:

  • importing the ‘python debugger’ with import pdb
  • inserting pdb.set_trace() where ever you want to break out of the code

This then launches the debugger where you can inspect objects and variables.

You can then control progress with the following commands (plus loads more):

  • s(tep) run the next line or n(ext) complete the next function
  • unt(il) run upto the specified line number
  • r(eturn) continue until the current function returns
  • c(ontinue) continue until a breakpoint is reached

For example I have a bit of code like this but it is failing to extract the materials id as I was expecting. Perhaps because I haven’t correctly modelled the structure of my complex object.

I can use the debugger like this:

import pdb

def extract_materials_id_from_complex_object(object):
  pdb.set_trace()
  return object['materials'['id']]

The python debugger will be revealed when I call this function with for example extract_materials_id_from_complex_object(doc) and I can directly inspect the object. In this case it highlights that I need to deal with a list rather than accessing the id directly.

(Pdb) object
{'materials': [{'text': 'Figure', 'id': '1'}]}