Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use the Python debugger

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.

like image 401
crystalattice Avatar asked Oct 08 '08 07:10

crystalattice


People also ask

Why you should use the Python debugger?

The Python debugger provides a debugging environment for Python programs. It supports setting conditional breakpoints, stepping through the source code one line at a time, stack inspection, and more.

Why should you learn to use pdb?

pdb is part of Python's standard library, so it's always there and available for use. This can be a life saver if you need to debug code in an environment where you don't have access to the GUI debugger you're familiar with.

Is Python good for debugging?

Debugging in Python is facilitated by pdb module (python debugger) which comes built-in to the Python standard library. It is actually defined as the class Pdb which internally makes use of bdb(basic debugger functions) and cmd (support for line-oriented command interpreters) modules.

What can you do with a debugger?

A debugger is a program that allows you to step through another program one line at a time. This is very useful when trying to identify incorrect code and analyze how a program "flows". Key concepts include: Breakpoints, Stepping, and Viewing data.


2 Answers

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.
like image 160
S.Lott Avatar answered Oct 26 '22 10:10

S.Lott


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.
like image 37
Sridhar Iyer Avatar answered Oct 26 '22 11:10

Sridhar Iyer