为什么IPython没有为我编写的模块提供完整的回溯?(Why isn't IPython giving me a full traceback for a module I've written?)

我很困惑为什么,当我写的模块中的函数中出现错误时,IPython没有向我显示导致错误的函数中的行的完整回溯。

注意:我并不是对这个特定错误的原因感到困惑,而是为什么IPython没有向我展示原因。

我的模块名为module.py ,它包含函数function ,在函数function下面写了一个if __name__ == '__main__'块。 (模块和功能名称已经更改,以保护无辜者的身份 - 或者可能不是那么无辜。)

这是我在提出错误时得到的回溯。 (注意缺少关于function哪一行导致错误的信息。)

In [1]: import module as m In [2]: call = m.function('hello') --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-2-ec0c1e40ec8c> in <module>() ----> 1 call = m.function('hello') /home/module.py in function(greeting) TypeError: join() takes exactly one argument (2 given)

I'm confused about why, when an error is raised in a function within a module I've written, IPython doesn't show me a full traceback with the line in the function that caused the error.

Note: I'm not confused about the cause of this particular error, but about why IPython isn't showing me the cause.

My module is called module.py and it contains the function function, underneath which is written an if __name__ == '__main__' block. (Module and function names have been changed to protect the identities of the innocent -- or maybe not so innocent.)

Here's the traceback I get when an error is raised. (Note the lack of information about which line in function caused the error.)

In [1]: import module as m In [2]: call = m.function('hello') --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-2-ec0c1e40ec8c> in <module>() ----> 1 call = m.function('hello') /home/module.py in function(greeting) TypeError: join() takes exactly one argument (2 given)

最满意答案

你尝试使用%xmode吗?

In [2]: %xmode? Type: Magic function Definition: %xmode(self, parameter_s='') Docstring: Switch modes for the exception handlers. Valid modes: Plain, Context and Verbose. If called without arguments, acts as a toggle.

如果你仔细观察,下面的两个例子是不同的,但是长尾追回的差异更明显:

In [8]: raise ValueError('Foo') --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-8-05e81bf5c607> in <module>() ----> 1 raise ValueError('Foo') global ValueError = undefined ValueError: Foo

普通模式

In [9]: xmode Exception reporting mode: Plain In [10]: raise ValueError('Foo') Traceback (most recent call last): File "<ipython-input-10-05e81bf5c607>", line 1, in <module> raise ValueError('Foo') ValueError: Foo

Did you try with %xmode ?

In [2]: %xmode? Type: Magic function Definition: %xmode(self, parameter_s='') Docstring: Switch modes for the exception handlers. Valid modes: Plain, Context and Verbose. If called without arguments, acts as a toggle.

if you look carefully the 2 following example are different, but difference is more visible with long tracebacks :

In [8]: raise ValueError('Foo') --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-8-05e81bf5c607> in <module>() ----> 1 raise ValueError('Foo') global ValueError = undefined ValueError: Foo

Plain mode

In [9]: xmode Exception reporting mode: Plain In [10]: raise ValueError('Foo') Traceback (most recent call last): File "<ipython-input-10-05e81bf5c607>", line 1, in <module> raise ValueError('Foo') ValueError: Foo为什么IPython没有为我编写的模块提供完整的回溯?(Why isn't IPython giving me a full traceback for a module I've written?)

我很困惑为什么,当我写的模块中的函数中出现错误时,IPython没有向我显示导致错误的函数中的行的完整回溯。

注意:我并不是对这个特定错误的原因感到困惑,而是为什么IPython没有向我展示原因。

我的模块名为module.py ,它包含函数function ,在函数function下面写了一个if __name__ == '__main__'块。 (模块和功能名称已经更改,以保护无辜者的身份 - 或者可能不是那么无辜。)

这是我在提出错误时得到的回溯。 (注意缺少关于function哪一行导致错误的信息。)

In [1]: import module as m In [2]: call = m.function('hello') --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-2-ec0c1e40ec8c> in <module>() ----> 1 call = m.function('hello') /home/module.py in function(greeting) TypeError: join() takes exactly one argument (2 given)

I'm confused about why, when an error is raised in a function within a module I've written, IPython doesn't show me a full traceback with the line in the function that caused the error.

Note: I'm not confused about the cause of this particular error, but about why IPython isn't showing me the cause.

My module is called module.py and it contains the function function, underneath which is written an if __name__ == '__main__' block. (Module and function names have been changed to protect the identities of the innocent -- or maybe not so innocent.)

Here's the traceback I get when an error is raised. (Note the lack of information about which line in function caused the error.)

In [1]: import module as m In [2]: call = m.function('hello') --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-2-ec0c1e40ec8c> in <module>() ----> 1 call = m.function('hello') /home/module.py in function(greeting) TypeError: join() takes exactly one argument (2 given)

最满意答案

你尝试使用%xmode吗?

In [2]: %xmode? Type: Magic function Definition: %xmode(self, parameter_s='') Docstring: Switch modes for the exception handlers. Valid modes: Plain, Context and Verbose. If called without arguments, acts as a toggle.

如果你仔细观察,下面的两个例子是不同的,但是长尾追回的差异更明显:

In [8]: raise ValueError('Foo') --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-8-05e81bf5c607> in <module>() ----> 1 raise ValueError('Foo') global ValueError = undefined ValueError: Foo

普通模式

In [9]: xmode Exception reporting mode: Plain In [10]: raise ValueError('Foo') Traceback (most recent call last): File "<ipython-input-10-05e81bf5c607>", line 1, in <module> raise ValueError('Foo') ValueError: Foo

Did you try with %xmode ?

In [2]: %xmode? Type: Magic function Definition: %xmode(self, parameter_s='') Docstring: Switch modes for the exception handlers. Valid modes: Plain, Context and Verbose. If called without arguments, acts as a toggle.

if you look carefully the 2 following example are different, but difference is more visible with long tracebacks :

In [8]: raise ValueError('Foo') --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-8-05e81bf5c607> in <module>() ----> 1 raise ValueError('Foo') global ValueError = undefined ValueError: Foo

Plain mode

In [9]: xmode Exception reporting mode: Plain In [10]: raise ValueError('Foo') Traceback (most recent call last): File "<ipython-input-10-05e81bf5c607>", line 1, in <module> raise ValueError('Foo') ValueError: Foo