python-debugger(Python)

Loading...

Use with The Python Debugger (pdb)


Note: If you are using Databricks Runtime 13.3 LTS or above, Databricks recommends that you use the built-in interactive debugger (AWS | Azure | GCP). The interactive debugger provides breakpoints, step-by-step execution, variable inspection, and more tools to help you develop code in notebooks more efficiently.


Notebooks running on Databricks Runtime 11.2 and above support The Python Debugger (pdb).

Some examples of using pdb in a notebook:

  • Use %debug to debug from the last exception. This is helpful when you run into an unexpected error and are trying to debug the cause (similar to pdb.pm()).
  • Use %pdb on to automatically start the interactive debugger after exceptions (but before program terminates).

Note that when you use these commands, you must finish using the debugger before you can run any other cell. Here are a few ways to exit the debugger:

  • c or continue to finish running the cell.
  • exit to throw an error and stop code execution.
  • Cancel the command by clicking Cancel next to the output box.

%debug : Post-mortem debugging

To use %debug in Databricks notebooks:

  1. Run commands in the notebook until an exception is raised.
  2. Run %debug in a new cell. The debugger starts running in the output area of the cell.
  3. To inspect a variable, type the variable name in the input field and press Enter.
  4. You can change context and perform other debugger tasks, like variable inspection, using these commands. For the complete list of debugger commands, see the pdb documentation. Type the letter and then press Enter.
    • n: next line
    • u: move up 1 level out of the current stack frame
    • d: move down 1 level out of the current stack frame
  5. Exit the debugger using one of the methods described in the first cell of this notebook.

Below is an example following these steps using %debug.

ZeroDivisionError: division by zero

    > <command-867591925311336>(4)getAccuracy() 2 def getAccuracy(self, correct, total): 3 # ... ----> 4 accuracy = correct / total 5 # ... 6 return accuracy
    ipdb>
    0
    ipdb>
    10
    ipdb>
    > <command-867591925311336>(15)printScore() 13 14 def printScore(self): ---> 15 print(f"You're score is: {self.system.getAccuracy(self.correct, self.total)}") 16 17 test = UserTest(
    ipdb>
    0
    ipdb>

    %pdb on : Pre-mortem debugging

    To use %pdb on in Databricks notebooks:

    1. Turn auto pdb on by running %pdb on in the first cell of your notebook.
    2. Run commands in the notebook until an exception is raised. The interactive debugger starts.
    3. To inspect a variable, type the variable name in the input field and press Enter.
    4. You can change context and perform other debugger tasks, like variable inspection, using these commands. For the complete list of debugger commands, see the pdb documentation. Type the letter and then press Enter.
      • n: next line
      • u: move up 1 level out of the current stack frame
      • d: move down 1 level out of the current stack frame
    5. Exit the debugger using one of the methods described in the first cell of this notebook.

    Below is an example following these steps using %pdb on.

      Automatic pdb calling has been turned ON

      Tests True > <command-867591925310549>(4)getAccuracy() 2 def getAccuracy(self, correct, total): 3 # ... ----> 4 accuracy = correct / total 5 # ... 6 return accuracy
      ipdb>
      *** ZeroDivisionError: division by zero
      ipdb>
      10
      ipdb>
      0
      ipdb>
      > <command-867591925310549>(13)<cell line: 13>() 9 10 ## test coverage 11 print("Tests") 12 print(system.getAccuracy(10, 100) == 0.1) ---> 13 print(system.getAccuracy(10, 0), 0)
      ipdb>
      ZeroDivisionError: division by zero