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

标签 python debugging jupyter-notebook pdb ipdb

当我尝试使用 %debug 在 Jupyter Notebook 中调试我的代码时,我遇到了“最旧的框架”。

> $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,)

其他包也是如此(例如 statsmodels 使用的 patsy)。

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

最佳答案

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

参见 https://github.com/ipython/ipython/issues/6251了解详情。我怀疑这是他们隐藏最后一帧(将在 ipython 中)的逻辑的结果,但没有确凿的证据证明这一点。

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

关于python - Jupyter Notebook 中的 %debug - 访问丢失的回溯帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50486598/

相关文章:

python - PyCharm 找不到模块 'math'

eclipse - "Errors exist in the active configuration..."调试时的Eclipse对话框

python - Jupyter 获取任意笔记本单元格内容

python - 无法在 pymongo 中使用格式显示结果

python - 在 Jupyter Notebook 中运行 TensorFlow

python - PyQt5:在运行时更新标签

python - Caffe 中的图像分类总是返回相同的类

Python:将列表分配给多维数组元素

c - 为什么我的 C 程序总是给出 1 错误?

ios - 显示来自 Core Data ManagedObjectContext 的属性