Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I interactively debug exceptions in Python using something besides IDLE?

When I'm using IDLE to run a script, if the script encounters an exception and execution stops, then I'm left with a interactive shell, which I can use to investigate the application state at the time of the exception. This is really nice, but otherwise I find IDLE lacking as an editor. Is there a way I can get this "drop to interactive shell on exceptions" behavior without using IDLE?enter image description here

like image 876
Buttons840 Avatar asked Mar 20 '12 16:03

Buttons840


People also ask

How do I use Idle’s debugger?

The main interface to IDLE’s debugger is the Debug Control window, or the Debug window for short. You can open the Debug window by selecting Debug→Debugger from the menu in the interactive window.

How to debug Python code?

You can use the basic principles you used here with a number of different debugging tools. You’re now well equipped to start debugging your Python code. You also got some practice debugging a faulty function using a four-step process for identifying and removing bugs: Guess where the bug is located. Set a breakpoint and inspect the code.

How do you fix a buggy line of code in Python?

Set a breakpoint and inspect the code by stepping through the buggy section one line at a time, keeping track of important variables along the way. Identify the line of code, if any, with the error and make a change to solve the problem.

How does the interactive window work in the debugger?

The interactive window also displays the output from having run the line with print () the first time through the loop. Each time you press the Go button, the debugger runs the code continuously until it reaches the next breakpoint.


3 Answers

I suggest using eclipse with pydev. It has many debug options, I dont see any advantage in using a shell for debugging. Try it and you may, I say.

like image 78
WeaselFox Avatar answered Sep 21 '22 16:09

WeaselFox


Run your script as follows:

python -m pdb myscript.py

The console will show you:

> /home/user/dir/myscript.py(2)<module>()

-> first_line(of_my_script) (Pdb)

Type continue

Wait for things to explode:

TypeError: invalid type comparison
Uncaught exception. Entering post mortem debugging
Running 'cont' or 'step' will restart the program
> /home/user/problemscript.py(567)na_op()
-> raise TypeError("invalid type comparison")
(Pdb)

From here on out, you're basically in a MUD and a surprising number of the standard commands apply.

Type where or w to see where you are on the stack:

(Pdb) w
-> return df[df['type']=='dev'][['Dist','Count']].as_matrix()
  /home/user/core/ops.py(603)wrapper()
-> res = na_op(values, other)
> /home/user/core/ops.py(567)na_op()
-> raise TypeError("invalid type comparison")

See that little > arrow? That's where we are in the stack.

Use list or l to look around:

(Pdb) list
564               try:
565                   result = getattr(x, name)(y)
566                   if result is NotImplemented:
567  >>                     raise TypeError("invalid type comparison")
568               except (AttributeError):
569  ->                 result = op(x, y)
570   
571           return result
572   
573       def wrapper(self, other):
574           if isinstance(other, pd.Series):

To move around in the stack continue MUDing and use up (u) or down (d).

Use args (a) to examine what arguments the current function was called with:

(Pdb) args
dat = array([], shape=(0, 3), dtype=float64)
dev_classes = {81, 82, 21, 22, 23, 24, 31}

Use p to print out the contents of a variable (or pp to pretty-print (or handle your character's basic needs)):

(Pdb) p df
Empty DataFrame
Columns: [Dist, type, Count]
Index: []

Use interact to enter the code at the current point in the stack. Ctrl+D brings you back in to PDB.

Go forth! It will take many brave and mighty adventurers to turn back the assembled goblin hordes, now surrounding the city. Will you be the one to defeat the Goblin King, to reclaim the land for the races?

like image 26
Richard Avatar answered Sep 17 '22 16:09

Richard


python -i yourscript will drop to an interactive shell when yourscript exits. At this point you can run:

>>> import pdb
>>> pdb.pm()

...and get an interactive debugging shell.

See the PDB documentation.

like image 22
Charles Duffy Avatar answered Sep 18 '22 16:09

Charles Duffy