I want to set breakpoints using pdb and then run the loop in the program until that breakpoint. and then after checking the values keep continuing (only stopping at that point) until the loop ends. how do i do it?
You can run your program into pdb from the command line by running
python -m pdb your_script.py
This will stop execution at line 1, now set breakpoint with b linenumber e.g. b 25. Now type run it will run the program until that break point.
If you can modify the code, one way is to add this line where you want to break:
import pdb; pdb.set_trace()
Related
If I run the toy script below as shown:
import sys
sys.path.append('/usr/share/anki')
import aqt
app = aqt._run(argv=['/usr/bin/anki', '--profile=test'], exec=False)
# breakpoint()
print(repr(aqt.mw.col))
aqt.mw.cleanupAndExit()
...I get the following output, which is not right:
$ python3 /tmp/ankiq.py
None
If I uncomment the commented statement, however, and re-run the modified script, I get the correct output (eventually):
$ python3 /tmp/ankiq.py
> /tmp/ankiq.py(8)<module>()
-> print(repr(aqt.mw.col))
(Pdb) c
<anki.collection._Collection object at 0x7f32ec1417b8>
I would like to avoid the need for the breakpoint() statement (and for having to hit c whenever I want to run such code).
My guess is that, when the breakpoint() statement is commented out, the print statement happens before aqt.mw has been fully initialized.
(I tried replacing the breakpoint() statement with time.sleep(1), but when I run the script with this modification, it hangs before ever printing any output.)
Q: How can I modify the toy script above so that, by the time the print statement executes, aqt.mw.col has its correct value?
It seems that calling aqt._run(*args, exec=False) returns a QApplication object - but without starting its event-loop. To manually process pending events, you could therefore try calling app.processEvents().
From the comments, it appears the exact solution is as follows:
while aqt.mw.col is None:
app.processEvents()
I think I figured how this is happening but I just want to know why it's happening.
I accidentally found that every time I call pdb.set_trace(), it seems to erase all the breakpoints that I've set. I was just wondering why that is. I couldn't find any explanation on Google. I had set several breakpoints but midway through debugging I noticed they had all disappeared. It wasn't until I ran a simple script with 2 pdb.set_trace() statements did I notice that if I ran pdb.set_trace(), breakpoints seemed to get deleted. Hopefully someone has an explanation for me.
Example pseudocode:
import pdb
pdb.set_trace()
print (a)
print (b)
print (c)
print (d)
pdb.set_trace()
print (e)
print (f)
At print (a), I set a break point for line print (c) and line
(print f).
I hit 'b' so that it shows my breakpoints (both of which
are there)
I hit 'c' for it to continue, which it does until print
(c), where it stops. I check again and the 2 breakpoints are still
there I hit 'c' and it proceeds until it reaches the second
pdb.set_trace().
At this point when I check again, I have 0
breakpoints.
After playing around some more with this, it seems like every time I execute pdb.set_trace() in my script, it will reset the breakpoints in the script, which is why they disappear like this. If I just have 1 pdb.set_trace() line in my script, the breakpoints appear to stick until the end of the execution of the script so I will be more mindful and not put multiple pdb.set_trace() statements in my script when debugging.
I debugging in PyCharm.
I insert a breakpoint at the line:
p_result = input('Who will win: ')
Now usually the debugger stops without executing the line where the breakpoint is. However this time I have to make an input before the thread is suspended.
In GDB, users can define some actions that run automatically when a breakpoint is hit. For example,
b foo.cpp:100
comm 1
p x
end
Does pdb have the similar function?
Yes. In https://docs.python.org/2/library/pdb.html, you can find:
commands [bpnumber]
Specify a list of commands for breakpoint number bpnumber. The commands themselves appear on the following lines. Type a line containing just ‘end’ to terminate the commands
I have a Python script that loads a CSV file of data and then runs functions on each line using a for loop. It all works great apart from consistently at item number 247 it exits. No error message, I just get returned to a command prompt.
I have tried entering debug statement to narrow down what is happening bu I am still no clearer as there is no error message.
Is there a way to run Windows Python is some sort of verbose mode so I can watch it running and see why my loop is stopping?
You could use the pdb module, with a selective if condition to invoke it for row containing item 247 only. Something like below, which will enter the interactive debug mode for the case when its not working:
for line in csvfile:
if row_item == 247:
import pdb; pdb.set_trace()
# regular processing here
From there, you can step into the function to understand what is not working.