问题
I'm quite new with debugging directly with pdb and I am having some issues debugging my Django application. Here is what I'm doing:
python -m pdb manage.py runserver
(pdb) b core/views.py:22
Breakpoint 2 at /Users/raphaelcruzeiro/Documents/Projects/pdb_test/core/views.py:22
(Pdb) c
However the execution passes directly through the breakpoint. Am I missing some command? The manual doesn't elaborate on setting a breakpoint anymore than this.
回答1:
I've been through the same problem.
Try something like python -m pdb ./manage.py runserver --nothreading --noreload 127.0.0.1:8080
. It solved the issue for me.
It seems that breakpoints with PDB are thread-specific, and the --nothreading
and --noreload
options are necessary to avoid some forking that may confuse PDB. This is also why set_trace
works, as it's called directly inside the thread of interest.
回答2:
I usually prefer set_trace()
in the source itself, that way the dev server will reload when added/removed, and I don't need to stop and start it again. For example:
def get_item(request):
import pdb; pdb.set_trace()
When the view is accessed, pdb will kick in.
回答3:
When I've seen this problem in the past, it's usually because someone has set the breakpoint on a line that is not actually connected to a Python statement that is run. For example, blank lines, comment lines, the wrong part of a multi-line statement.
回答4:
One strange thing I have noticed is that the PDB prompt repeats your previous action when you repeatedly hit enter. Moreover, if you hit enter whilst your program is running, PDB buffers the input and applies it once the prompt appears. In my case, I was running a program using PDB c(ontinue). My program was writing lots of debug info to stdout during initialization, so I hit enter a couple of times to separate the already written output from the output that was going to be written once the breakpoint was triggered. Then, when I triggered the breakpoint via some external action, PDB would stop at the breakpoint but then apply a 'buffered enter' which repeated the c(ontinue) action. Once I stopped hitting enter it all started working normally.
This may sound a bit strange, and I haven't investigated this issue much, but it solved the problem for me. Maybe it helps someone else.
来源:https://stackoverflow.com/questions/7617066/pdb-wont-stop-on-breakpoint