Why is sys.exit() causing a traceback?
Asked Answered
J

5

6

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)

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
>>>

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

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

Jestude answered 1/2, 2018 at 20:29 Comment(3)
You cannot. It's the way sys.exit() is implemented.Bipack
@GhilasBELHADJ Your link uses the Python 2 documentation. You should repost with the Python 3 docs.Jestude
note that this isn't really a traceback. SystemExit exceptions don't have tracebacksDurbar
I
9

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.

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.

Illimani answered 1/2, 2018 at 20:38 Comment(3)
when using os._exit(0), you can exit "-i" without any tracebackPhiz
@Matt.St: os.exit is a drastic move that exits without any cleanup. No finally, __exit__, atexit, __del__, etc. It's not likely to be appropriate here.Illimani
you are absolutely right @user2357112, but in case he want drastic move, it can workPhiz
Z
4

No exception shown:

python exit.py 

and your program is terminated.

Run with -i option for interactive (inspect interactively after running script) and the exception is shown:

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

because the interpreter keeps running.

exit([status])

Exit the interpreter by raising SystemExit(status).

Zerk answered 1/2, 2018 at 20:39 Comment(0)
J
1

Because under the hood, sys.exit(0) raises a SystemExit exception.

Further reading here

what you want is:

os._exit(1)

Jackijackie answered 1/2, 2018 at 20:35 Comment(1)
os._exit(1) will suppress the traceback, but it will also suppress all exit cleanup and exit immediately. This can cause data loss.Illimani
P
1

As Matt_G says, sys.exit(0) is exactly the same as raising SystemExit(), which can be caught in higher level, which in your case is happening since you are using -i.

If you want to exit without traceback, there is os._exit(0) which calls a "C function" and exit immediately even in -i mode

as @user2357112 told me, os._exit(0) is drastic move that exits without any cleanup. No finally, __exit__, atexit, __del__, etc.

Phiz answered 1/2, 2018 at 20:48 Comment(0)
I
0

Because you are using the -i option. Try it without that and you won't get a stack trace.

$ python ptest.py
$ python -i ptest.py
Traceback (most recent call last):
  File "ptest.py", line 3, in <module>
    sys.exit(0)
SystemExit: 0
Ironing answered 1/2, 2018 at 20:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.