【发布时间】:2018-07-12 06:39:49
【问题描述】:
根据How to exit from Python without traceback?,在 Python 脚本中调用 sys.exit() 应该会静默退出而不会回溯。
import sys
sys.exit(0)
但是,当我在 Windows 7 上使用python -i "exit.py" (or from Notepad++) 的命令行启动脚本时,会显示SystemExit 异常的回溯。
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 命令行运行时显示回溯?
(作为参考,我在 Windows 7 上使用 Python 3.6.4)
【问题讨论】:
-
你不能。这是
sys.exit()的实现方式。 -
@GhilasBELHADJ 您的链接使用 Python 2 文档。您应该使用Python 3 docs 重新发布。
-
请注意,这并不是真正的回溯。
SystemExit异常没有回溯