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.


Asked by: Lana868 | Posted: 24-09-2021






Answer 1

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.

Answered by: Jared266 | Posted: 25-10-2021



Answer 2

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.

Answered by: Madaline853 | Posted: 25-10-2021



Answer 3

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.

Answered by: Rafael491 | Posted: 25-10-2021



Answer 4

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.

Answered by: Michelle255 | Posted: 25-10-2021



Answer 5

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.

Answered by: Aida257 | Posted: 25-10-2021



Answer 6

You might want to take a look at this other SO post:

Why is debugging better in an IDE?

It's directly relevant to what you're asking about.

Answered by: Ryan167 | Posted: 25-10-2021



Similar questions

debugging - Is there a Python debugger that runs wholly under Emacs?

Is it possible to set up breakpoints and print variables in a Python program, all from within Emacs?


debugging - Is there a python 3.x debugger like gdb

Closed. This question does not meet Stack Overflow guid...


debugging - How does python debugger work?

I have a basic understanding of how a debugger work but that is in context of compiled languages. How does a debugger like pdb work? At a very high level, I am looking for something that can explain internals of pdb or in general "debugging interpreted languages" I googled up but couldn't get any doc. This question might be too broad but link to some basic documents would allow me to study...


debugging - pydev debugger usage as a python module

pydev debugger is a python debugger used by pydev and pycharm. It seems much more powerful than pdb. Its code is available (https://github.com/fabioz/PyDev.Debugger), and it is easy to install through pip. The package name is pydevd. However, there is no ...


debugging - Python debugger pdb not found on macOS High Sierra

The PythonDebuggingTools documentation says that the Python debugger pdb is "part of all Python installations", yet I can't find it under macOS High Sierra: pdb: command not found Is it supposed to be installed as normal par...


debugging - Python Debugger on VS Code 2019 hangs

I'm trying to use the Python debugger on VS Code 2019 on my Mac, but it looks like it is causing the program to hang indefinitely (the print statement never gets executed). If I run the program without the debugger, everything works. I don't have any breakpoints configured either. Here is my code: #!/usr/bin/env python3 dummy = input("Enter something: ") print(dummy)


VS Code / Python / Debugging pytest Test with the Debugger

How can I get VS Code to put me into the debugger at the point of failure when running tests with pytest? Pytest catches all errors and asserts, and VS code invokes the debugger only on uncaught errors (I can change this to raised exception, but then it stops on everything raised under a try). I tried to set --pdb as pytest argument, but this leads to errors: ======================...


debugging - Python debugger in spyder stops at line 2

when i am trying to debug my code the debugger stops at line 2 and doenst respond to any commands (like go to next line). I am using python 3.9.7. This is what the console looks like: If I try to stop the debugger this happens:


debugging - how to load a module within python debugger

This looks like something simple but I could not find the answer so far - I have just learnt python and need to start learning pdb. In my module I have the usual if __name__ == __main_ trick to execute some code when the module is run as a program. So far I have been running it via python -m mymod arg1 arg2 syntax Now I want to do exactly the same thing from inside pdb. Nor...


debugging - How can I step to use the python debugger to break at every function call?

I want to closely monitor the chain of function calls which are called from a certain function. import pdb; pdb.set_trace() res = api.InsertVideoEntry(video_entry, video) I'm looking for a way to easily see that api.insertVideoEntry(video_entry, video) calls foo() which calls bar() which calls baz(), Here's a really crude diagram ...


debugging - How do I inspect return values in the Winpdb Python debugger, without having to modify the source code?

pdb, the default Python debugger, has an undocumented (?) retval command that prints the return value of the current function if you already computed it and are one step away from returning back to the caller frame. Is there similar functionality in the graphical Winpdb debugger? I can...


debugging - File is locked by Python debugger

I have a problem with understanding a strange file locking behavior in Python Debugger. I have a 2TB image file, which my script reads. Everything works perfect, until I want to read the same file with a different hex editor. If the file is opened in hex editor before I start my script, everything is fine. If I try to open the file during script paused at breakpoint, my system almost hangs and becomes very slow. I ...


debugging - Python Debugger which supports Black Boxing?

If I use the debugger, most of the times I just want to see what the interpreter does in my code. I want to step over all code of the framework and libraries I use. AFAIK this is called Black Boxing. How can I do this with Python ipdb or an other Python debugger? Imagine this: I use a orm framework which I trust, and don't want to debug. cut_hair_method(orm_object.us...


debugging - Python logging and Pydev debugger?

Edit : Using Liclipse 1.2.1 instead of 1.3.0 or 1.4.0 is working fine. Changelog indicate both Pydev 3.9.1 and Eclipse 4.4.1 updates for 1.3.0. Seems to break logging debug. Using Liclipse and Pydev debugger (and CPython) with the following code sample, getting that error : logging.config.dictConfig(config) File "C:\Python27\lib\logging\config.py", line 794, in dictC...


debugging - How to decide where Python debugger stops and which line is to be blamed?

Background: I write Squish GUI tests in Python. I tried to make test code as Pythonic and DRY as I could and hence I moved all repeating code to separate classes / modules. Problem definition: test.verify or assert statement tells the debugger to stop at the very line where the statement is and that's in most cases the module with details of single test step. This line is shown in ...


debugging - Exiting Python Debugger ipdb

I use ipdb fairly often in a way to just jump to a piece of code that is isolated i.e. it is hard to write a real script that uses it. Instead I write a minimal test case with mocking and jump into it. Exemplary for the workflow: def func(): ... import ipdb ipdb.set_trace() ... def test_case(): ... func() ... Then, invoke py.test t...


debugging - How can I start the Python debugger from the command line without a script argument?

I'd like to poke around with some Python syntax, but don't want to write a full script. In Perl I would say perl -demo What's the equivalent in Python? The easiest I've found is python -im pdb /dev/null but that seems long-winded, especially when showing someone else. Edit1: For those confused about the debugger, here is the help a...


debugging - Is there a Python debugger that runs wholly under Emacs?

Is it possible to set up breakpoints and print variables in a Python program, all from within Emacs?


python - Django debugging with Emacs

I found a lot of info about how to debug simple Python programs with Emacs. But what if I want to debug a Django application? I run the development server and I would like to somehow attach to the process from Emacs and then set breakpoints, etc. Similar to Visual Studio's "attach to process". How to do that?


python - Debugging pylons in Eclipse under Ubuntu

I am trying to get pylons to debug in Eclipse under Ubuntu. Specifically. I am not sure what to use for the 'Main Module' on the Run configurations dialog. (this is a similar question on stackoverflow, but I think it applies to windows as I can't find paster-script.py on my system) Can anyone help?


debugging - What are good ways to make my Python code run first time?

Closed. This question needs to be more focused. It ...


linux - Python memory debugging with GDB

We have a Linux application that makes use of OpenSSL's Python bindings and I suspect it is causing random crashes. Occasionally, we see it crash with the message: Python Fatal Error: GC Object already tracked which would appear to be either a programming error on the part of the library, or a symptom of memory corruption. Is there any way to know the last line of Python source...


Debugging a running python process

Is there a way to see a stacktrace of what various threads are doing inside a python process? Let's suppose I have a thread which allows me some sort of remote access to the process.


debugging - How do you pass script arguments to pdb (Python)?

I've got python script (ala #! /usr/bin/python) and I want to debug it with pdb. How can I pass arguments to the script? I have a python script and would like to debug it with pdb. Is there a way that I can pass arguments to the scripts?


debugging - Stop python from closing on error

In python when running scripts is there a way to stop the console window from closing after spitting out the traceback?


django - Python: Memory leak debugging

I have a small multithreaded script running in django and over time its starts using more and more memory. Leaving it for a full day eats about 6GB of RAM and I start to swap. Following http://www.lshift.net/blog/2008/11/14/tracing-python-memory-leaks I see this as the most common types (with only 800M of memory used):


debugging - Compiling python modules with DEBUG defined on MSVC

Python rather stupidly has a pragma directive in its include files that forces a link against python26_d.lib when the DEBUG preprocessor variable is defined. This is a problem because the python installer doesn't come with python26_d.lib! So I can't build applications in MSVC in debug mode. If I temporarily #undef DEBUG for just one file I get many complaints about incons...


debugging - Python code seems to be getting executed out of order

At work I have a programming language encoded in a database record. I'm trying to write a print function in python to display what the record contains. This is the code I'm having trouble with: # Un-indent the block if necessary. if func_option[row.FRML_FUNC_OPTN] in ['Endif', 'Else']: self.indent = self.indent - 1 # if this is a new line, indent it. if len(self.formulatext...






Still can't find your answer? Check out these communities...



PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python



top