简体   繁体   中英

Terminating a Python Program

What command do you use in python to terminate a program?

ie the equivalent of "end" in basic, or "quit" in BASH.

I see that "break" takes you out of a loop, and "quit" is all tied up with "class" stuff that I do not comprehend yet. i tried

import sys
sys.exit()

but it will display following error:

Traceback (most recent call last):
  File "C:\Documents and Settings\....\Desktop\current file_hand\Python_1.py", line 131, in <module>
    sys.exit()
SystemExit

is there any solution for it.

sys.exit() raises the SystemExit exception.

If you don't catch that exception the program ends.

Since you're getting that output, I'm not sure what is happening, but I guess that you're catching all exceptions and printing them yourself:

try:
    ...
except:
    print exception somehow
    raise

If that's the case, don't do that. catch Exception instead:

...
    except Exception:
        ...

That way you won't catch things not meant to be catched (like SystemExit ).

sys.exit(error_code)

Error_code will be 0 for a normal exit, 1 or some other positive number for an exit due to an error of some kind, eg the user has entered the wrong parameters.

sys.exit() "is undefined on some architectures", (although it worked when I tried it on my Linux box!)

The official python docs explains this more fully.

It's an extremely good idea for all your programs and scripts to follow the return code convention; 0 for OK, something else for error, (normally 1)

For example, if you run a script which grabs some data out of a database; returning 0 and no output, means the database is perfectly fine there's just nothing in it (or nothing matching your query). returning 1 and no output means there is a fault with the database, the whole process should abort, because to continue would corrupt the other system too.

You should also consider alternatives to exiting directly. Often return works just as well if you wrap code in a function. (Better, in fact, because it avoids sys.exit() weirdness.)

def main():
    ...do something...
    if something:
        return               # <----- return takes the place of exit
    ...do something else...

main()
sys.exit() #to exit the program
return     #to exit from a function

import sys

sys.exit(0)

Try running a python interpreter out of your IDE. In my Windows installation the simple command line python.exe , both options work:

>>> import sys
>>> sys.exit()

or

>>> raise SystemExit

In your case, your error is likely that you have a bare except block that is catching the SystemExit exception, like this:

import sys
try:
  sys.exit(return_code)
except:
  pass

The correct way to fix your problem is to remove the except: portion, and instead just catch the Exceptions you expect to be possibly raised. For example:

try:
  # Code which could raise exceptions
except (NameError, ValueError):
  # Do something in case of NameError or ValueError, but
  # ignore other exceptions (like SystemExit)

However, if you really wanted your program to exit, the following code will work:

import os
try:
  os._exit(return_code)
except:
  pass

This will exit even with the except: clause, as it just directly calls the C function of the same name which kills your process. This is not recommended unless you know what you are doing, since this will not call cleanup handlers or flush open IO buffers.

I met this problem on Windows where I needed to close ParaView (I could not use pvbatch or pvpython, because of OpenGL initialization, and sys.exit does not work)

Below is the specific solution for Windows.

# import os module
import os
 
# delete given process
os.system('wmic process where name="Process_Name" delete')

# for example
os.system('wmic process where name="paraview.exe" delete')

Source of this solution is here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM