Jupyter Notebook 中的 %debug - 访问丢失的回溯帧

%debug in Jupyter Notebook - accessing missing traceback frames

当我尝试使用 %debug 在 Jupyter Notebook 中调试我的代码时,我点击了 "Oldest frame"。

> $HOME/anaconda3/envs/py36/lib/python3.6/site-packages/matplotlib/axes/_base.py(244)_xy_from_xy()
    242         if x.shape[0] != y.shape[0]:
    243             raise ValueError("x and y must have same first dimension, but "
--> 244                              "have shapes {} and {}".format(x.shape, y.shape))
    245         if x.ndim > 2 or y.ndim > 2:
    246             raise ValueError("x and y can be no greater than 2-D, but have "

ipdb> up
> $HOME/anaconda3/envs/py36/lib/python3.6/site-packages/matplotlib/axes/_base.py(385)_plot_args()
    383             x, y = index_of(tup[-1])
    384 
--> 385         x, y = self._xy_from_xy(x, y)
    386 
    387         if self.command == 'plot':

ipdb> up
> $HOME/anaconda3/envs/py36/lib/python3.6/site-packages/matplotlib/axes/_base.py(407)_grab_next_args()
    405                 return
    406             if len(remaining) <= 3:
--> 407                 for seg in self._plot_args(remaining, kwargs):
    408                     yield seg
    409                 return

ipdb> up
*** Oldest frame

问题是异常的回溯要长得多:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-52-c0be78c78358> in <module>()
----> 1 _ = plotSensitivity('inverse_corelation_uniform_log_boost_serial_smooth_lite', 1)

<ipython-input-51-c479a5eeae49> in plotSensitivity(dataset, threshold)
     75                      truePositives * 100,
     76                      label=method,
---> 77                      ls=LS[method[0]])
     78 
     79         plt.xscale('log')

$HOME/anaconda3/envs/py36/lib/python3.6/site-packages/matplotlib/pyplot.py in plot(*args, **kwargs)
   3315                       mplDeprecation)
   3316     try:
-> 3317         ret = ax.plot(*args, **kwargs)
   3318     finally:
   3319         ax._hold = washold

$HOME/anaconda3/envs/py36/lib/python3.6/site-packages/matplotlib/__init__.py in inner(ax, *args, **kwargs)
   1896                     warnings.warn(msg % (label_namer, func.__name__),
   1897                                   RuntimeWarning, stacklevel=2)
-> 1898             return func(ax, *args, **kwargs)
   1899         pre_doc = inner.__doc__
   1900         if pre_doc is None:

$HOME/anaconda3/envs/py36/lib/python3.6/site-packages/matplotlib/axes/_axes.py in plot(self, *args, **kwargs)
   1404         kwargs = cbook.normalize_kwargs(kwargs, _alias_map)
   1405 
-> 1406         for line in self._get_lines(*args, **kwargs):
   1407             self.add_line(line)
   1408             lines.append(line)

$HOME/anaconda3/envs/py36/lib/python3.6/site-packages/matplotlib/axes/_base.py in _grab_next_args(self, *args, **kwargs)
    405                 return
    406             if len(remaining) <= 3:
--> 407                 for seg in self._plot_args(remaining, kwargs):
    408                     yield seg
    409                 return

$HOME/anaconda3/envs/py36/lib/python3.6/site-packages/matplotlib/axes/_base.py in _plot_args(self, tup, kwargs)
    383             x, y = index_of(tup[-1])
    384 
--> 385         x, y = self._xy_from_xy(x, y)
    386 
    387         if self.command == 'plot':

$HOME/anaconda3/envs/py36/lib/python3.6/site-packages/matplotlib/axes/_base.py in _xy_from_xy(self, x, y)
    242         if x.shape[0] != y.shape[0]:
    243             raise ValueError("x and y must have same first dimension, but "
--> 244                              "have shapes {} and {}".format(x.shape, y.shape))
    245         if x.ndim > 2 or y.ndim > 2:
    246             raise ValueError("x and y can be no greater than 2-D, but have "

ValueError: x and y must have same first dimension, but have shapes (5,) and (500,)

其他包也是如此(例如 patsystatsmodels 使用)。

我如何从调试器中使用我的代码访问帧?

不幸的是,我认为您在 ipython 中遇到了一个长期存在的错误,它无法通过生成器调试调用堆栈。

有关详细信息,请参阅 https://github.com/ipython/ipython/issues/6251。我怀疑这是他们隐藏最后一帧(将在 ipython 内)的逻辑的结果,但没有确凿的证据。

在他们修复它之前,您可以使用 import pdb; pdb.pm() 而不是魔法来解决这个问题。