ipython debugger: full traceback on interactive pdb?

2024/9/20 6:09:11

I recently switched from ipython0.10 to ipython0.11. In ipython0.11, I only see a small snippet of the full traceback when the python debugger engages (i.e. using %pdb), whereas in ipython0.10 I'd see the full traceback. As far as I can tell, the full traceback is not directly accessible from the pdb command line - you can navigate through it with 'u' but can't see it directly.

So, is there any way to show the full traceback? Such as a configuration parameter?

Or, even more usefully, is there any way to have ipython just show the Exception that was caught, rather than showing where in the code it was caught?

EDIT: Example:

In [1]: pdb
Automatic pdb calling has been turned ONIn [2]: 1/0
> <ipython-input-2-05c9758a9c21>(1)<module>()-1 1/0ipdb> q
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
/Users/adam/<ipython-input-2-05c9758a9c21> in <module>()
----> 1 1/0ZeroDivisionError: integer division or modulo by zero

I'd like to see the ZeroDivisionError before q'ing out of the pdb.

Answer

is there any way to have ipython just show the Exception that wascaught, rather than showing where in the code it was caught?

You could use sys.excepthook:

import sysdef exc_hook(type, value, traceback):print typesys.excepthook = exc_hook

From the sys module documentation:

sys.excepthook(type, value, traceback)

This function prints out a given traceback and exception tosys.stderr.

When an exception is raised and uncaught, the interpreter callssys.excepthook with three arguments, the exception class, exceptioninstance, and a traceback object. In an interactive session thishappens just before control is returned to the prompt; in a Pythonprogram this happens just before the program exits. The handling ofsuch top-level exceptions can be customized by assigning anotherthree-argument function to sys.excepthook.

sys.__displayhook__
sys.__excepthook__

These objects contain the original values of displayhook andexcepthook at the start of the program. They are saved so thatdisplayhook and excepthook can be restored in case they happen to getreplaced with broken objects.


You can also try starting ipython with the --xmode option set to Plain

From IPython reference:

$ ipython [options] files--xmode=<modename>

Mode for exception reporting.

Valid modes: Plain, Context and Verbose.

Plain: similar to python’s normal traceback printing.

Context: prints 5 lines of context source code around each line in the traceback.

Verbose: similar to Context, but additionally prints the variables currently visible where the exception happened (shortening theirstrings if too long). This can potentially be very slow, if you happento have a huge data structure whose string representation is complexto compute. Your computer may appear to freeze for a while with cpuusage at 100%. If this occurs, you can cancel the traceback withCtrl-C (maybe hitting it more than once).

Here are some example usages. Notice the difference in lines for each traceback:

--xmode=Plain:

[ 19:55 jon@hozbox ~/SO/python ]$ ipython --xmode=Plain ipython-debugger-full-traceback-on-interactive-pdb.py 
------------------------------------------------------------
Traceback (most recent call last):File "ipython-debugger-full-traceback-on-interactive-pdb.py", line 2, in <module>1 / 0
ZeroDivisionError: integer division or modulo by zero

--xmode=Context:

[ 19:55 jon@hozbox ~/SO/python ]$ ipython --xmode=Context ipython-debugger-full-traceback-on-interactive-pdb.py 
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)/home/jon/SO/python/ipython-debugger-full-traceback-on-interactive-pdb.py in <module>()1 
----> 2 #!/usr/bin/python3 1 / 04 5 ZeroDivisionError: integer division or modulo by zero

--xmode=Verbose:

[ 19:54 jon@hozbox ~/SO/python ]$ ipython --xmode=Verbose ipython-debugger-full-traceback-on-interactive-pdb.py 
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)/home/jon/SO/python/ipython-debugger-full-traceback-on-interactive-pdb.py in <module>()1 
----> 2 #!/usr/bin/python3 1 / 04 5 ZeroDivisionError: integer division or modulo by zero

And without specifying a .py file:

--xmode=Plain:

[ 19:55 jon@hozbox ~/SO/python ]$ ipython --xmode=PlainIn [1]: 1 / 0
------------------------------------------------------------
Traceback (most recent call last):File "<ipython console>", line 1, in <module>
ZeroDivisionError: integer division or modulo by zero

--xmode=Context:

[ 20:03 jon@hozbox ~/SO/python ]$ ipython --xmode=ContextIn [1]: 1 / 0
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)/home/jon/SO/python/<ipython console> in <module>()ZeroDivisionError: integer division or modulo by zero

--xmode=Verbose:

[ 20:01 jon@hozbox ~/SO/python ]$ ipython --xmode=VerboseIn [1]: 1 / 0
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)/home/jon/SO/python/<ipython console> in <module>()ZeroDivisionError: integer division or modulo by zero

Using the Python debugger.

https://en.xdnf.cn/q/72529.html

Related Q&A

How to get all users in a list Twitter API?

Is there a way to access all members in a list? Currently, I can only see the first 20 members? Specifically, Im using python and tweepy.

Python adding a blank/empty column. csv

Hello I have a database that i am trying to make a .csv file quickly from.my data looks like this.Song_Name,File_Name,Artist_Name,Artist_ID Song1,filename1,artistname,artist001 Song1,filename1,artistna…

Displaying Radio buttons horizontally in matplotlib

I am using the matplotlib.widgets to create radio buttons in my widgets, the buttons coming are stacked vertically, I would like them to be stacked horizontally.MVCE:import matplotlib.pyplot as plt fro…

Python MemoryError on large array

This is the python script that Im trying to run:n = 50000000000 ##50 billion b = [0]*n for x in range(0,n):b[x] = random.randint(1,899999)... But the output Im getting is:E:\python\> python sort.py…

How to generate a PDF with non-ascii characters using from_string from python-pdfkit

Im struggling to generate just a simple PDF with non-ascii characters using Python 3.5.2, python-pdfkit and wkhtmltox-0.12.2.This is the easiest example I could write:import pdfkit html_content = u<…

Minimum window of days required to travel all cities

This is an interesting question that I came across in a coding challenge:There are k cities and n days. A travel agent is going to show you city k on day n. Youre supposed to find the minimum number of…

Force Nosetests to Use Python 2.7 instead of 3.4

Ive been learning Python using version 3.4. I recently started learning Web.py so have been using Python 2.7 for that, since web.py not supported in Python 3.4. I have nose 1.3.4 module installed for …

Python regex not to match http://

I am facing a problem to match and replace certain words, not contained in http:// Present Regex: http://.*?\s+This matches the pattern http://www.egg1.com http://www.egg2.com I need a regex to matc…

how can I maintain sequence of my list using set?

In [1]: l1 = [a,2,3,0,9.0,0,2,6,b,a]In [2]: l2 = list(set(l1))In [3]: l2 Out[3]: [a, 0, 2, 3, 6, 9.0, b]Here you can see the the list l2 is falling with different sequence then the original l1, I need …

How to reference a dict object?

I have a Python dict object d. d = {a: 1, b: 2, c: 3}. My problem is really simple. I want to reference a variable to the elements of d. For example, something like:In[1]: p = d[a] >>> p = 1 I…