Back to Blog Home

5 Different Ways To Exit Python Program

Varun Shenoy| Python | 6 months, 3 weeks



 

There are times when you need to stop execution of your python script or terminate the program midway. Python provides couple of different ways to achieve it, each with it’s own nuances and use cases. 

 

Lets take a look at different ways available to us for exiting a python script or aborting it’s execution. 

 

1. quit()

 

quit() is a built-in python command you can use to terminate a script while you  are working in a Python interpreter. 

You can simply run it like:

quit()

No need to import any library or module, just simply call the command.

 

quit() works by simply raising a SystemExit exception behind the scene which closes the current script which spawned it in the first place. 

 

Although quit() looks really simple and works quite efficiently while aborting a script from interpreter, but it should never be used in actual production grade code. Reason is, quit() depends upon site module, which might or might not be loaded in your production environment. site module is loaded during startup, except if -s command line option is mentioned and it adds several constants to the built-in namespace. So it works fine during interactive shell use cases but might fail in production code. 

 

Printing quit function will give you the help message:

 

>>> print (quit)
Use quit() or Ctrl-Z plus Return to exit
>>>

 

So use quit() function only in interpreter. 

 

2. exit()

 

exit() is exactly similar to quit() function and infact exit() is just another name for quit() function.

You can check this by printing exit()

>>> print (exit)
Use quit() or Ctrl-Z plus Return to exit
>>>

 

It is the same help message as that of quit() function. So both quit() and exit() are aliases.

 

It can be called anywhere within the python script to prematurely exit it. No need for any imports or anything. 

exit()

 

exit() also works in a similar fashion to quit() by raising a SystemExit exception and depends upon site module, so it should only be used while working in interactive python interpreter and not in any code you wish to put in production. 

 

3. os._exit()

 

os._exit() is something you can call a ‘hard’ way to exit the python script because it kills the entire process and not just the current single thread. os._exit() should only be used for special cases because it doesn’t call any cleanup handlers or flushing stdio buffers, etc.

 

You would need to import os module for calling this command.

 

import os
os._exit()

 

One of the use cases of os._exit() function is to terminate a python script in when child processes need to be killed after an os.fork().

 

4. sys.exit()

 

sys.exit() is the recommended way for exiting the python script. You will need to import sys module to use it. 

 

import sys
sys.exit()

 

sys.exit() works by raising a SystemExec exception, similar to quit() or exit(), but it doesn’t require site module and instead uses sys module which is always present. Hence sys.exit() is the most preferred way to terminate python script in production code.

 

It is possible to intercept the exit attempt of the python script at outer level by putting try in place. 

 

5. SystemExit

 

You can also abort the python script by simply raising the SystemExit directly

 

raise SystemExit

 

 

So these were some common ways to terminate a python script. So if you want to exit the program or script, most preferred way to use would be the sys.exit() function. 

 

 



Join 1000+ People Who Subscribe to Weekly Blog Updates

Back to Blog Home