Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to break in interactive mode on an exception to debug a Python script?

I would like to debug a Python package, let say it is Sphinx.

when I do sphinx-build everything works utile a point I have an exception. Instead of the Traceback I would like to start an interactive session from there in which I can debug my code.

Is that possible?

For instance I am looking for something like:

$ python-debug sphinx-build arg args 
... 
...
Traceback (oops)
...
>>> print("Oh yeah, now we can play...")
like image 691
nowox Avatar asked Nov 16 '25 20:11

nowox


2 Answers

Perhaps pdb might be of use to you. Simply invoke it like so where you want to break into the debugger:

import pdb; pdb.set_trace()

For more details please see the pdb documentation: https://docs.python.org/3/library/pdb.html

Edit: Perhaps more relevant to your question: pdb can be invoked on the command-line like so: python3 -m pdb myscript.py

like image 160
temp123 Avatar answered Nov 18 '25 08:11

temp123


I think this can help you to figure out where to place a debugger:

  • Check the line number in the Traceback of the module.
  • Then open the module in any of the text editor by going into the module location.
  • Keep the debugger before that line.

If your are using python3.7 Then you can simply call this function breakpoint() or you can also do this by using import pdb; pdb.set_trace().

like image 37
Thrilok Kumar Pallaki Avatar answered Nov 18 '25 09:11

Thrilok Kumar Pallaki