Exiting python script is pure

I am trying to exit a python script without the appearance of an annoying error message:

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

I tried a lot of things, but no one worked. Here is 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 should not be anywhere close to this code, it was just an example of what I tried

+3
source share
1 answer

When I run this in the Python console (under Windows or Linux), I do not receive an 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 kind of IDE, is that the case?

+5
source

All Articles