为什么sys.exit()引起回溯? [英] Why is sys.exit() causing a traceback?

查看:176
本文介绍了为什么sys.exit()引起回溯?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 如何从Python退出而无需追溯? ,在其中调用sys.exit() Python脚本应无提示地退出,而不会产生回溯.

According to How to exit from Python without traceback?, calling sys.exit() in a Python script should exit silently without a traceback.

import sys
sys.exit(0)

但是,当我使用python -i "exit.py"在Windows 7上从命令行启动脚本时,(或从Notepad ++ ),则会显示对 SystemExit 异常的回溯./p>

However, when I launch my script from the command line on Windows 7 with python -i "exit.py", (or from Notepad++), a traceback for a SystemExit exception is displayed.

U:\>python -i "exit.py"
Traceback (most recent call last):
  File "exit.py", line 2, in <module>
    sys.exit(0)
SystemExit: 0
>>>

为什么sys.exit()从Windows命令行运行时显示回溯?

Why is sys.exit() displaying a traceback when run from the Windows command line?

(供参考,我在Windows 7上使用Python 3.6.4)

(For reference, I am using Python 3.6.4 on Windows 7)

推荐答案

您正在使用-i标志运行Python. -i禁止对SystemExit进行通常的特殊处理sys.exit引发;由于抑制了特殊处理,因此Python将执行常规的异常处理,该异常处理将显示回溯.

You're running Python with the -i flag. -i suppresses the usual special handling of the SystemExit exception sys.exit raises; since the special handling is suppressed, Python performs the normal exception handling, which prints a traceback.

可以说,-i应该只禁止特殊处理的退出"部分,而不引起回溯打印.您可以提出错误报告;我没有看到任何现有的相关报告.

Arguably, -i should only suppress the "exit" part of the special handling, and not cause a traceback to be printed. You could raise a bug report; I didn't see any existing, related reports.

这篇关于为什么sys.exit()引起回溯?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆