views:

472

answers:

5

Since Python is a dynamic, interpreted language you don't have to compile your code before running it. Hence, it's very easy to simply write your code, run it, see what problems occur, and fix them. Using hotkeys or macros can make this incredibly quick.

So, because it's so easy to immediately see the output of your program and any errors that may occur, I haven't uses a debugger tool yet. What situations may call for using a real debugger vs. the method I currently use?

I'd like to know before I get into a situation and get frustrated because I don't know how to fix the problem.

+1  A: 

Any time you want to inspect the contents of variables that may have caused the error. The only way you can do that is to stop execution and take a look at the stack.

pydev in Eclipse is a pretty good IDE if you are looking for one.

Simon
+5  A: 

I use pdb for basic python debugging. Some of the situations I use it are:

  • When you have a loop iterating over 100,000 entries and want to break at a specific point, it becomes really helpful.(conditional breaks)
  • Trace the control flow of someone else's code.
  • Its always better to use a debugger than litter the code with prints.
  • Normally there can be more than one point of failures resulting in a bug, all are not obvious in the first look. So you look for obvious places, if nothing is wrong there, you move ahead and add some more prints.. debugger can save you time here, you dont need to add the print and run again.
Sridhar Iyer
+4  A: 

Usually when the error is buried in some function, but I don't know exactly what or where. Either I insert dozens of log.debug() calls and then have to take them back out, or just put in:

import pdb
pdb.set_trace ()

and then run the program. The debugger will launch when it reaches that point and give me a full REPL to poke around in.

John Millikin
+7  A: 

In 30 years of programming I've used a debugger exactly 4 times. All four times were to read the core file produced from a C program crashing to locate the traceback information that's buried in there.

I don't think debuggers help much, even in compiled languages. Many people like debuggers, there are some reasons for using them, I'm sure, or people wouldn't lavish such love and care on them.

Here's the point -- software is knowledge capture.

Yes, it does have to run. More importantly, however, software has meaning.

This is not an indictment of your use of a debugger. However, I find that the folks who rely on debugging will sometimes produce really odd-looking code and won't have a good justification for what it means. They can only say "it may be a hack, but it works."

My suggestion on debuggers is "don't bother".

"But, what if I'm totally stumped?" you ask, "should I learn the debugger then?" Totally stumped by what? The language? Python's too simple for utter befuddlement. Some library? Perhaps.

Here's what you do -- with or without a debugger.

  1. You have the source, read it.
  2. You write small tests to exercise the library. Using the interactive shell, if possible. [All the really good libraries seem to show their features using the interactive Python mode -- I strive for this level of tight, clear simplicity.]
  3. You have the source, add print functions.
S.Lott
A debugger is not a replacement for a brain, I agree, but neither is the contrary true. You have to think about the problem first, of course. But many times, a good debugger lets you verify your hypothesis much faster than any other method would.
Kena
@Kena: I find that to be untrue in Python. Interactive Python is so easy to use that the debugger seems superfluous.
S.Lott
Well, I'm not sure exactly where to draw the line between the debugger and the interactive console. In my book, it's all a continuum of "figuring out why this code doesn't do what it's supposed to do", whether I call a snippet directly in the console or fire up the console at a key point using pdb. I just happen to like the second option better for the kind of bugs I encounter, but whatever works for you...
Kena
A: 

I find it very useful to drop into a debugger in a failing test case.

I add import pdb; pdb.set_trace() just before the failure point of the test. The test runs, building up a potentially quite large context (e.g. importing a database fixture or constructing an HTTP request). When the test reaches the pdb.set_trace() line, it drops into the interactive debugger and I can inspect the context in which the failure occurs with the usual pdb commands looking for clues as to the cause.

Ben Godfrey