18

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.

2
  • 3
    The documentation also says Changed in version 3.2: .pdbrc can now contain commands that continue debugging, such as continue or next. Previously, these commands had no effect. I tried it with python 3.3, and it does exactly what you want as stated in your question. What version of python are you running?
    – snapshoe
    Commented Oct 17, 2013 at 0:18
  • @snapshoe not new enough, tends to be 2.7 because libraries aren't moving to 3x quickly enough. Thanks for that though. :)
    – coiax
    Commented Oct 19, 2013 at 11:31

6 Answers 6

15
+100

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.

3
  • When I start python -m pdb somescript.py it will instantly start the PDB even when no exception was raised. I think think the OP wanted PDB only to pop up when something went wrong, that's what ipython somescript.py --pdb does. Commented Oct 17, 2013 at 8:04
  • Ah, and this is where I discover I put my foot into my mouth; that's a documentation bug; at the very least it should mention that debugging starts immediately, as well as entering PM mode when you continue to an unhandled exception. Commented Oct 17, 2013 at 9:13
  • Adding to this that if it is desired to also pass arguments to yourmodule.py, one can separate them with -- so that they are not passed to ipython, e.g. ipython --pdb yourmodule.py -- --modarg1 one --modarg2 two Commented Aug 30 at 19:50
10

Since Python 3.2 you can use the following:

python3 -m pdb -c c script.py
1
  • This is the best answer for Python 3+. Doesn't require setting default behavior in .pdbrc.
    – jayelm
    Commented Jul 27, 2017 at 23:07
2

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.

1

that's probably not the way to do to it.

you should modify/extend the existing pdb.py. the code looks like this:

while True:
    try:
        pdb._runscript(mainpyfile)
        if pdb._user_requested_quit:
            break
        print "The program finished and will be restarted" 
    except:
        traceback.print_exc()
        print "Uncaught exception. Entering post mortem debugging"
        print "Running 'cont' or 'step' will restart the program"
        t = sys.exc_info()[2]
        pdb.interaction(None, t)
        print "Post mortem debugger finished. The "+mainpyfile+" will be restarted"

it looks like you could replace `pdb._runscript(mainpyfile)' with something like

runpy.run_module(sys.argv[0], run_name="__main__", alter_sys=True)

(from PEP 338).

not a complete solution, and i haven't tested it, but maybe it helps.

edit: but for a simpler solution, for a single script, your script probably ends with something like:

if __name__ == '__main__':
    call_main_function()

you just replace it it

if __name__ == '__main__':
    try:
        call_main_function()
    except:
        import pdb
        t = sys.exc_info()[2]
        pdb.interaction(None, t)

(last code stolen from pdb.py, that's exactly what it does when it gets an unhandled exception).

0

Are you certain that it's not actually running? From the docs

When invoked as a script, pdb will automatically enter post-mortem debugging if the program being debugged exits abnormally. After post-mortem debugging (or after normal exit of the program), pdb will restart the program. Automatic restarting preserves pdb’s state (such as breakpoints) and in most cases is more useful than quitting the debugger upon program’s exit.

emphasis mine.

When I ran my program trying to figure out why it did the same thing (displayed the very first line of code), I kept trying different things and then finally I noticed this line:

The program finished and will be restarted

If your program is, in fact, running without exception and then restarting, it will appear to start from the very beginning of your program.

3
  • I wrote a 2 line while loop script, and ran it as OP suggested with python -m pdb script.py, and I'm certain it didn't run.
    – snapshoe
    Commented Oct 16, 2013 at 22:40
  • @snapshoe: of course PDB doesn't kick in, because there is no exception being raised. The OP doesn't specify when he does expect pdb to start tracing again. Commented Oct 17, 2013 at 6:57
  • Yes, he did-- when he launches the script: "The problem is that it instantly goes:... 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?"
    – snapshoe
    Commented Oct 18, 2013 at 1:51
-2

pdb does not have a command that facilitates this for you. You can try using an IDE like PyCharm of PyDev. You can do what Corley Brigman suggested, or you can simple use an IDE, that makes life a lot easier.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.