How do you debug in Python? Coming from a Matlab and R user. I'm already aware of pdb.

Chris Angelico rosuav at gmail.com
Wed Jan 27 04:34:17 EST 2021


On Wed, Jan 27, 2021 at 8:21 PM Chris Green <cl at isbd.net> wrote:
>
> Skip Montanaro <skip.montanaro at gmail.com> wrote:
> > CW> How do you troubleshooting/debugging in Python?
> >
> > GE> Mostly I read exception trace and read the code and think about it.
> > GE> If that doesn't work, I add some print() or syslog() calls.
> >
> > CW> I know hardcore computer scientists would tell me about Python debugger.
> >
> > GE> I've been writing Python for 20+ years. I've never tried a debugger.
> >
> > Agree with Grant on these points. I certainly use gdb to debug C code
> > (like the interpreter), but for Python code, tracebacks and print
> > statements pretty much take care of things. I know pdb has been around
> > for a long while and many people like IDEs, but I've not generally
> > found them all that useful. I'm stuck in the 90s I guess.
> >
> Me too! :-)
>

Me three :)

I'm of the opinion that everyone should get some experience with the
most fundamental form of debugging: trigger the problem with logging
active. Sometimes that means reproducing a known problem, other times
it means predicting an unknown problem and just always having some
useful logging. (That's why your boot sequence inevitably creates a
bunch of logs of various forms.)

IIDPIO: If In Doubt, Print It Out... just keep that console noisy
until you're certain the information won't help you.

Some problems are easy to reproduce. You figure out how to make it
happen, you adjust your code, you tinker. Sometimes the tinkering will
involve a debug harness (like pdb), other times it just requires a few
more print() calls, and either way, great! But other problems aren't.
Try debugging a weird issue with your web site that shows up only once
every two weeks (or worse), and only ever occurs once. You can't run
your web server in a debug harness because the problem simply doesn't
happen on your local computer. Your only option is to try to log more
stuff and wait for the problem to happen again...

ChrisA


More information about the Python-list mailing list