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

When I use IDLE to run a script, if the script detects an exception and execution stops, I leave an interactive shell that I can use to examine the state of the application at the time of the exception. This is really nice, but otherwise I don’t see that IDLE is missing as an editor. Is there a way I can get the "drop to interactive shell on exceptions" behavior without using IDLE? enter image description here

+4
source share
6 answers

Run the 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 something 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 you are in MUD and an amazing amount of standard commands apply.

Enter 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") 

Look at the little arrow > ? This is where we are on the stack.

Use the 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 on the stack, continue MUDing and use up ( u ) or down ( d ) ..

Use args ( a ) to check with which arguments the current function is called:

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

Use p to print the contents of the variable (or pp for beautiful printing (or to cope with your character’s basic needs)):

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

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

Walk straight! Many brave and powerful adventurers will need to reverse the collected goblin hordes that now surround the city. Will you defeat the Goblin King to reclaim the land for the races?

+2
source

I suggest using eclipse with pydev . It has a lot of debugging options, I don’t see any advantage in using the shell for debugging. Try it and you can, I say.

+6
source

You can use the pdb module .

 import pdb try: i = 0 i = i + 'a string' except Exception, err: pdb.set_trace() 
+3
source

python -i yourscript will exit into the 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 .

+1
source

IMHO. If you are working in python and not using IPython , you are wasting your time (literally).

In it, you can turn pdb on or off by simply typing the magic pdb command. The new qtconsole (my favorite) and laptop options make this killer environment even better.

+1
source

Run the script inside the python command interpreter ( import it), and when there is an exception, import pdb; pdb.pm() import pdb; pdb.pm() to get the debugger at the point after the exception occurs.

+1
source

Source: https://habr.com/ru/post/1402481/


All Articles