Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When running Python's pdb as a script, how do I autostart the script?

Tags:

python

pdb

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.

like image 616
coiax Avatar asked Sep 03 '13 18:09

coiax


People also ask

How do I run a pdb in a Python script?

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().

How do you continue execution in pdb?

@turtle: continue should "Continue execution, only stop when a breakpoint is encountered", according to the docs.

How does Python pdb work?

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.


3 Answers

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.

like image 75
Bastian Venthur Avatar answered Oct 17 '22 05:10

Bastian Venthur


Since Python 3.2 you can use the following:

python3 -m pdb -c c script.py
like image 7
orlp Avatar answered Oct 17 '22 04:10

orlp


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.

like image 2
snapshoe Avatar answered Oct 17 '22 04:10

snapshoe