28

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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Raphael
  • 7,972
  • 14
  • 62
  • 83

5 Answers5

24

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.

Gustavo Meira
  • 2,875
  • 3
  • 21
  • 33
  • 1
    Any ideas on how to do this in other modes (in particular, test)? --nothreading appears to be runserver-only. – Jeff Trull Aug 28 '15 at 19:32
10

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.

mkriheli
  • 1,788
  • 10
  • 18
  • 3
    The problem with this alternative is that it pollutes the source code – Raphael Oct 01 '11 at 00:59
  • Guess it's a matter of opinion. Anyway, It's just during debug, of course this statement is removed once the debugging session is over (plus, one does not need to remember where to break, or update the command line if the source case has been edited since). – mkriheli Oct 01 '11 at 01:08
  • Sure but there's a great rick I believe that this statement will be forgotten. All I want is to setup a simple breakpoint through pdb. It shouldn,t be this hard and there should be more documentation for the tool... – Raphael Oct 01 '11 at 01:10
  • Ok, my problem seems to be bigger than I thought. I tried your alternative and again the debugger did't stop. Just to be sure, this is how I call the debugger for a Django application right? python -m pdb manage.py runserver – Raphael Oct 01 '11 at 01:15
  • 1
    I've managed to make PDB stop on set_trace (for some reason the code wasn't been reloaded automatically after the edit). Unfortunately I still haven't managed to make pdb stop on a brakepoint created with the b command. – Raphael Oct 01 '11 at 01:54
  • This answer works for me, but I would rather use a user-friendly IDE, liek PyCharm Community Edition https://www.jetbrains.com/pycharm/ – Mawg says reinstate Monica Sep 27 '16 at 10:15
4

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.

Michael Hoffman
  • 32,526
  • 7
  • 64
  • 86
2

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.

Ibby
  • 21
  • 1
0

I ran into this issue when writing a neural network in PyTorch. Similar to the accepted answer, the problem was that my DataLoader spun off multiple threads. Removing the num_workers argument allowed me to debug on a single thread.

    train_loader = DataLoader(
        train_dataset,
        batch_size=batch_size,
        num_workers=16, # <-------Remove this argument
        pin_memory=True
    )

If you are running into this issue, a simple fix would be to track down where in your code you are using multiprocessing, and adjust it to run a single thread.

Jacob Stern
  • 3,758
  • 3
  • 32
  • 54