exit and quit are not guaranteed to be in the global namespace scope, but the exception SystemExit is, which is what sys.exit() raises. Use this code if importing sys is undesirable: raise SystemExit Answer from Deleted User on reddit.com
🌐
Reddit
reddit.com › r/learnpython › why is sys.exit() recommended over exit() or quit()
r/learnpython on Reddit: Why is sys.exit() recommended over exit() or quit()
July 21, 2020 -

In most questions asking how to stop code the recommended answer is sys.exit() or raising an exception. Why is exit() not suggested given it is simpler, not requiring import sys, and it does the same thing underneath?

e.g. https://www.reddit.com/r/learnpython/comments/hv7phs/how_do_i_stop_a_code/?utm_medium=android_app&utm_source=share)

Top answer
1 of 4
1006

Let me give some information on them:

  1. quit() simply raises the SystemExit exception.

Furthermore, if you print it, it will give a message:

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

This functionality was included to help people who do not know Python. After all, one of the most likely things a newbie will try to exit Python is typing in quit.

Nevertheless, quit should not be used in production code. This is because it only works if the site module is loaded. Instead, this function should only be used in the interpreter.

  1. exit() is an alias for quit (or vice-versa). They exist together simply to make Python more user-friendly.

Furthermore, it too gives a message when printed:

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

However, like quit, exit is considered bad to use in production code and should be reserved for use in the interpreter. This is because it too relies on the site module.

  1. sys.exit() also raises the SystemExit exception. This means that it is the same as quit and exit in that respect.

Unlike those two however, sys.exit is considered good to use in production code. This is because the sys module will always be there.

  1. os._exit() exits the program without calling cleanup handlers, flushing stdio buffers, etc. Thus, it is not a standard way to exit and should only be used in special cases. The most common of these is in the child process(es) created by os.fork.

Note that, of the four methods given, only this one is unique in what it does.

Summed up, all four methods exit the program. However, the first two are considered bad to use in production code and the last is a non-standard, dirty way that is only used in special scenarios. So, if you want to exit a program normally, go with the third method: sys.exit.


Or, even better in my opinion, you can just do directly what sys.exit does behind the scenes and run:

raise SystemExit

This way, you do not need to import sys first.

However, this choice is simply one on style and is purely up to you.

2 of 4
156

The functions* quit(), exit(), and sys.exit() function in the same way: they raise the SystemExit exception. So there is no real difference, except that sys.exit() is always available but exit() and quit() are only available if the site module is imported (docs).

The os._exit() function is special, it exits immediately without calling any cleanup functions (it doesn't flush buffers, for example). This is designed for highly specialized use cases... basically, only in the child after an os.fork() call.

Conclusion

  • Use exit() or quit() in the REPL.

  • Use sys.exit() in scripts, or raise SystemExit() if you prefer.

  • Use os._exit() for child processes to exit after a call to os.fork().

All of these can be called without arguments, or you can specify the exit status, e.g., exit(1) or raise SystemExit(1) to exit with status 1. Note that portable programs are limited to exit status codes in the range 0-255, if you raise SystemExit(256) on many systems this will get truncated and your process will actually exit with status 0.

Footnotes

* Actually, quit() and exit() are callable instance objects, but I think it's okay to call them functions.

Discussions

Choose among: `builtins.quit`, `builtins.exit`, and `sys.exit`
I’m new to Python. It seems that they almost do the same thing (?) May I ask, in what scenarios are these functions respectively applicable? Why would one use this one instead of the other two? TIA :slight_smile: More on discuss.python.org
🌐 discuss.python.org
2
1
April 25, 2024
Can "quit" be made to quit? - Ideas - Discussions on Python.org
Likewise .quit has immediate effect in the sqlite3 repl. But in Python, typing quit doesn’t quit: >>> quit Use quit() or Ctrl-D (i.e. EOF) to exit Would it be reasonable to make quit actually quit? Perhaps the Quitter.__repr__ could invoke Quitter.__call__. More on discuss.python.org
🌐 discuss.python.org
3
March 22, 2023
Exiting with core.quit() or sys.exit()
Can I safely close the application with sys.exit(‘msg’) or is it necessary to call core.quit()? I am asking because I have seen a few examles where is sys.exit(‘msg’) used and I do not understand the difference · As you can see, .core.quit() calls sys.exit() itself, but before that, ... More on discourse.psychopy.org
🌐 discourse.psychopy.org
0
0
March 5, 2018
Return vs sys.exit()
Consider a program with threads and lots of asynchronous stuff. I have a main where at the end of it somebody has written “sys.exit(0)”. And in catching exceptions at some places there’s sys.exit(1). But I want to return some data at the end of main. If I use return statement above ... More on discuss.python.org
🌐 discuss.python.org
9
0
February 5, 2024
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-exit-commands-quit-exit-sys-exit-and-os-_exit
Python exit commands: quit(), exit(), sys.exit() and os._exit() - GeeksforGeeks
July 12, 2025 - The exit() in Python is defined as exit commands in python if in site.py and it works only if the site module is imported so it should be used in the interpreter only. It is like a synonym for quit() to make Python more user-friendly.
🌐
Codecademy
codecademy.com › article › python-exit-commands-quit-exit-sys-exit-os-exit-and-keyboard-shortcuts
Python Exit Commands: quit(), exit(), sys.exit(), os._exit() and Keyboard Shortcuts | Codecademy
It functions the same way as quit(), raising a SystemExit exception internally. It is intended for interactive use, such as within the Python shell. If used in scripts without importing the site module, it will raise a NameError. So, in practical terms, exit() doesn’t do anything differently than quit(), but it may be preferred in interactive scenarios where the word “exit” feels more intuitive than “quit”.
🌐
Python.org
discuss.python.org › python help
Choose among: `builtins.quit`, `builtins.exit`, and `sys.exit`
April 25, 2024 - I’m new to Python. It seems that they almost do the same thing (?) May I ask, in what scenarios are these functions respectively applicable? Why would one use this one instead of the other two? TIA :slight_smile:
🌐
Analytics Vidhya
analyticsvidhya.com › home › python exit commands: quit(), exit(), sys.exit() and os._exit()
Python Exit Commands: quit(), exit(), sys.exit() and os._exit()
August 8, 2024 - They are handy when you want to stop the program due to an error or when it has completed its task. You can also enroll in our free python course today. The quit() command in Python is a built-in function that ends the execution of a Python program.
🌐
Esri Community
community.esri.com › t5 › python-questions › sys-exit-versus-exit-to-terminate-a-standalone › td-p › 166732
Solved: sys.exit() versus exit() to terminate a standalone... - Esri Community
December 11, 2021 - SystemExit C:\EnvClones\arcgispro-py3-clone243\lib\site-packages\IPython\core\interactiveshell.py:3327: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D. warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)‍‍‍‍‍‍‍ · So using exit() is the clear favorite there. Is sys.exit() preferred in a stand alone script? That should just about do it.... Solved! Go to Solution. ... Some discussion on StackOverflow: Python exit commands - why so many and when should each be used?
Find elsewhere
🌐
Python.org
discuss.python.org › ideas
Can "quit" be made to quit? - Ideas - Discussions on Python.org
March 22, 2023 - Likewise .quit has immediate effect in the sqlite3 repl. But in Python, typing quit doesn’t quit: >>> quit Use quit() or Ctrl-D (i.e. EOF) to exit Would it be reasonable to make quit actually quit? Perhaps the Quitter.__repr__ could invoke Quitter.__call__.
🌐
Python.org
discuss.python.org › ideas
Can "quit" be made to quit? - Page 2 - Ideas - Discussions on Python.org
March 26, 2023 - I was just using Octave and its quit works just fine. Likewise .quit has immediate effect in the sqlite3 repl. But in Python, typing quit doesn’t quit: >>> quit Use quit() or Ctrl-D (i.e. EOF) to exit Would it be reaso…
🌐
DataCamp
datacamp.com › tutorial › how-to-exit-python-a-quick-tutorial
How to Exit Python: A Quick Tutorial | DataCamp
April 5, 2024 - However, there are times when I need to exit Python more programmatically, for instance when I’m working directly in the terminal or when I want a program to close Python automatically after running. In those cases, Python’s built-in exit functions are the way to go. Use exit() or quit() to terminate Python processes in casual code and sys.exit() in production code.
🌐
Zhihu
zhihu.com › en › answer › 3631994756
What is the difference between exit() and quit() in Python?
Can you accept that your future child will take the mother's surname · Actually, I still support it. If it's a two-way marriage, the economic burden on families with sons would be reduced by more than a little.Actually, I still support it. If it's a two-way marriage, the economic burden on ...
🌐
PsychoPy
discourse.psychopy.org › coding
Exiting with core.quit() or sys.exit() - Coding - PsychoPy
March 5, 2018 - Can I safely close the application with sys.exit(‘msg’) or is it necessary to call core.quit()? I am asking because I have seen a few examles where is sys.exit(‘msg’) used and I do not understand the difference · As you can see, .core.quit() calls sys.exit() itself, but before that, ...
🌐
YouTube
youtube.com › shorts › MLmgAps0SQk
YouTube
When do you use exit(), quit() or sys.exit()? I will explain the differences and when to use in this video.
Published   April 6, 2023
🌐
Python Forum
python-forum.io › thread-32660.html
How to programmatically exit without Traceback?
February 24, 2021 - This code ends by printing 4: import sys for i in range(10): if i == 5: sys.exit(0) print(i)When I enter sum_two(3.5,6), the following prints 'SystemExit' and then 'UserWarning: To exit: use 'exit', 'quit', or Ctrl-D. warn('To ex...
🌐
Quora
quora.com › What-is-the-difference-between-sys-exit-and-break-in-Python
What is the difference between sys.exit() and break in Python? - Quora
Answer: sys.exit( ): aborts the execution of the current program and passes control to the environment. break: breaks a loop (while or for), and prevents the loop from ending by not meeting the condition (while) or running out of set iterations ...
🌐
Inductive Automation
forum.inductiveautomation.com › general discussion
Exiting Early with sys.exit() - General Discussion - Inductive Automation Forum
June 22, 2010 - I often like to exit early from Jython functions I write by returning. This helps keep nested if conditions to a minimum. For instance, I’d check if data in arguments are valid and if they are not then return right away. But I couldn’t do this when writing event handlers because event handlers ...
🌐
Sololearn
sololearn.com › en › Discuss › 59547 › what-is-the-difference-between-quit-and-exit-commands-in-python
What is the Difference Between quit() and exit() commands in Python? | Sololearn: Learn to code for FREE!
python · 16th Sep 2016, 8:05 PM · Jay Halani · 1 AnswerAnswer · + 1 · Well quit() exits you from the program completly while exit() does the same thing. Quit is usually used outside a loop.
🌐
Naiveskill
naiveskill.com › home › complete python exit tutorial with examples in 2023
Complete python exit tutorial with examples in 2023 - Naiveskill
January 18, 2023 - exit() is a built-in function in both Python2 and Python3, while quit() is a built-in function only in Python2.