A good list of libraries, but please, don't use this in the middle of your code to set a break point: import pdb; pdb.set_trace(); There's a chance you forget this, check-in, and it ends in production. Use pdb facilities instead: $ python -m pdb Then set a breakpoint and continue: (Pdb) break : (Pdb) c This is trivial to automate from any editor or command line, so you don't even have to guess the path to the file. E…
Debugging Python Like a Boss
91–100 of 100 posts
Re: Debugging Python Like a Boss
#92One thing I love about much-maligned Tcl is its ability to connect a repl to a running program. Not just remote debugging of a halted program - you can actually inspect and alter variables and issue commands into the executing code. Is there a way to do that in, say, Python?
Re: Debugging Python Like a Boss
#93The most frustrating thing (experienced in both Javascript and Python) is the "oh uncaught exception? let me just quit everything" model. Most of the time, if I were just given an interactive prompt right then, I could spend 1 minute looking at local variables, maybe get a special stack trace variable to look at that, then be over with it. Instead I have to stick in some print statements and start everything over aga…
Re: Debugging Python Like a Boss
#94Earlier quoted context omitted.
And what if your bug is after the nth iteration of the loop, where the exact value of n is unknown (it's a bug, after all)? How many times are you going to set the variable and run the loop n times? Whereas with print-debugging and a grep you can filter for only unexpected/unusual output, and so narrow down to the likely cause, faster. Generalizing, of course. There are definitely cases where a debugger can be more e…
I'd set a conditional breakpoint: (gdb)> break Breakpoint 1 at .... (gdb)> cond 1 x cont And now, it will execute until 'x 42', then it triggers. Although, to be honest, I mostly use a debugger as an exploratory printf these days: (gdb)> call pretty_print(my_data) I do mainly use printf as the front line of debugging, though, with several debug switches on the command line. I follow up by jumping to a debugger once I…
Re: Debugging Python Like a Boss
#95The most frustrating thing (experienced in both Javascript and Python) is the "oh uncaught exception? let me just quit everything" model. Most of the time, if I were just given an interactive prompt right then, I could spend 1 minute looking at local variables, maybe get a special stack trace variable to look at that, then be over with it. Instead I have to stick in some print statements and start everything over aga…
There's a nice trick to enable this behavior for standard Python code run at the command line. Write the body of your code inside a main() function, then call it using the following toplevel block: if __name__ == "__main__": try: main() except KeyboardInterrupt: # allow ctrl-C raise except Exception as e: import sys, traceback, pdb print e type, value, tb = sys.exc_info() traceback.print_exc() pdb.post_mortem(tb) Thi…
$ python -m pdb yourscript.py
(Pdb) c
Will let you inspect the local scope after an uncaught exception.Re: Debugging Python Like a Boss
#96Earlier quoted context omitted.
And what if your bug is after the nth iteration of the loop, where the exact value of n is unknown (it's a bug, after all)? How many times are you going to set the variable and run the loop n times? Whereas with print-debugging and a grep you can filter for only unexpected/unusual output, and so narrow down to the likely cause, faster. Generalizing, of course. There are definitely cases where a debugger can be more e…
I'd set a conditional breakpoint: (gdb)> break Breakpoint 1 at .... (gdb)> cond 1 x cont And now, it will execute until 'x 42', then it triggers. Although, to be honest, I mostly use a debugger as an exploratory printf these days: (gdb)> call pretty_print(my_data) I do mainly use printf as the front line of debugging, though, with several debug switches on the command line. I follow up by jumping to a debugger once I…
Re: Debugging Python Like a Boss
#97One thing I love about much-maligned Tcl is its ability to connect a repl to a running program. Not just remote debugging of a halted program - you can actually inspect and alter variables and issue commands into the executing code. Is there a way to do that in, say, Python?
All Python debuggers allow you to execute statements, on top of navigating the stack.
In Tcl, the program can be running its event loop at normal speed, and you can inject commands into that event loop while it runs.
Re: Debugging Python Like a Boss
#98Earlier quoted context omitted.
I'd set a conditional breakpoint: (gdb)> break Breakpoint 1 at .... (gdb)> cond 1 x cont And now, it will execute until 'x 42', then it triggers. Although, to be honest, I mostly use a debugger as an exploratory printf these days: (gdb)> call pretty_print(my_data) I do mainly use printf as the front line of debugging, though, with several debug switches on the command line. I follow up by jumping to a debugger once I…
Backward stepping is cool. When I was checking out various Lisps some time ago, I saw in (IIRC) Franz Lisp that they had something like Visual Studio's Edit and Continue (maybe before VS did). Not a Lisper myself, though I've dabbled in it now and then and like it, so maybe that has been a feature of Lisps from much before - don't know.
http://msdn.microsoft.com/en-us/library/vstudio/bcew296c.asp...
Re: Debugging Python Like a Boss
#99Earlier quoted context omitted.
There's a nice trick to enable this behavior for standard Python code run at the command line. Write the body of your code inside a main() function, then call it using the following toplevel block: if __name__ == "__main__": try: main() except KeyboardInterrupt: # allow ctrl-C raise except Exception as e: import sys, traceback, pdb print e type, value, tb = sys.exc_info() traceback.print_exc() pdb.post_mortem(tb) Thi…
You don't need this trickery. $ python -m pdb yourscript.py (Pdb) c Will let you inspect the local scope after an uncaught exception.