Setting exit code in atexit callback
问题 Is there any way to set exit code in the function registered in atexit module and called on exit? The call to sys.exit(code) produces an error and does not set exit code to the desired value. d:\>python atexit_test.py Error in atexit._run_exitfuncs: Traceback (most recent call last): File "atexit_test.py", line 3, in myexit sys.exit(2) SystemExit: 2 d:\>echo %ERRORLEVEL% 0 The contents of atexit_test.py : def myexit(): import sys sys.exit(2) import atexit atexit.register(myexit) 回答1: I can