我对 IPython 异常给我的以下输出感到恼火:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-3-4c27956d23f6> in <module>()
----> 1 parameterStudyPageRankNibble()
/Users/cls/Desktop/LFR/scripts/scd_experiments.py in parameterStudyPageRankNibble()
AttributeError: 'tuple' object has no attribute 'values'
- 引发异常的行号实际上是
有帮助。为什么不在那里? /Users/cls/Desktop/LFR/scripts/scd_experiments.py不是执行脚本的路径。 IPython以某种方式混合在一起 脚本文件的路径和当前工作目录。
当抛出异常时,我该怎么做才能获得更多有用的信息?
请您参考如下方法:
这与将代码加载到 IPython 中的方式有关。 (如果您告诉我们如何重现您的问题,将会很有帮助。) 例如,如果我使用 emacs 的 M-x py-execute-region,代码区域会被复制到一个临时文件中,IPython 解释器会在临时文件上运行 execfile。
当以这种方式使用 emacs/IPython 时,没有有用的回溯。
如果相反,您导入脚本然后调用函数:
import scd_experiments as SE
SE.parameterStudyPageRankNibble()
然后您将看到有用的回溯消息。
演示:script.py
def foo():
1/0
如果我选择区域并调用 M-x py-execute-region,
In [15]: ## working on region in file /tmp/python-4126aNj.py...
In [16]: foo()
---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
<ipython-input-16-624891b0d01a> in <module>()
----> 1 foo()
/tmp/python-4126aNj.py in foo()
ZeroDivisionError: integer division or modulo by zero
但是如果我导入模块:
In [19]: import script
In [20]: script.foo()
---------------------------------------------------------------------------
ZeroDivisionError Traceback (most recent call last)
<ipython-input-20-3ae0d96fd279> in <module>()
----> 1 script.foo()
/home/unutbu/pybin/script.py in foo()
8
9 def foo():
---> 10 1/0
ZeroDivisionError: integer division or modulo by zero
