ACarter
ACarter

Reputation: 5707

Exiting a python script cleanly

I am trying to exit a python script without the annoying error message coming up:

Traceback (most recent call last):
  File "<pyshell#27>", line 3, in <module>
    sys.exit()
SystemExit

I have tried quite a few things, but none have worked. Here's an example:

while True:
    print "hi", #this just tests to see if I have exited.
    try:
        sys.exit()
    except SystemExit:
        print "Exited"

NB: The solution doesn't have to be anywhere close to this code, that was just an example of something I have tried

Upvotes: 3

Views: 11106

Answers (1)

Levon
Levon

Reputation: 143122

When I run this in the Python console (under Windows or Linux) I don't get any sort of error message when I use sys.exit(-1). However, iPython will give me

An exception has occurred, use %tb to see the full traceback.

SystemExit: -1

I suspect you are seeing error messages because you are working in some sort of IDE, is that the case?

Upvotes: 5

Related Questions