If I want to run a script and have pdb
catch any exceptions come up, I invoke it like so:
python -m pdb script.py
Or:
pdb script.py
The problem is that it stops at the debugging prompt right away:
> /home/coiax/Junkyard/script.py(1)<module>()
-> import sys
(Pdb)
And I have to type c
or continue
to get it to go. Is there any way of getting it to just load and start the script without initially asking if I want to set any breakpoints or whatever?
I swear I've read the pdb
module documentation, and tried making a .pdbrc file containing
c
but it doesn't start auto-magically.
Starting Python Debugger To start debugging within the program just insert import pdb, pdb. set_trace() commands. Run your script normally and execution will stop where we have introduced a breakpoint. So basically we are hard coding a breakpoint on a line below where we call set_trace().
@turtle: continue should "Continue execution, only stop when a breakpoint is encountered", according to the docs.
The module pdb defines an interactive source code debugger for Python programs. It supports setting (conditional) breakpoints and single stepping at the source line level, inspection of stack frames, source code listing, and evaluation of arbitrary Python code in the context of any stack frame.
What you want can trivially be achieved using ipython
:
ipython yourmodule.py --pdb
Whenever an exception is raised which is not caught properly (i.e. the program crashes), you instantly land in ipython's debugger at the point where the exception was raised. From there on you can move the stack up and down, inspect variables, etc. This is a huge time saver when developing with Python; I use it all the time.
Since Python 3.2 you can use the following:
python3 -m pdb -c c script.py
It works exactly as you describe and want it to work in python 3.3-- with the 'c' character in a .pdbrc file.
The pdb
module documentation says this was added in 3.2:
Changed in version 3.2: .pdbrc can now contain commands that continue debugging, such as continue or next. Previously, these commands had no effect.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With