Let me give some information on them:
quit()simply raises theSystemExitexception.
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.
exit()is an alias forquit(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.
sys.exit()also raises theSystemExitexception. This means that it is the same asquitandexitin 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.
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 byos.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.
Answer from user2555451 on Stack OverflowLet me give some information on them:
quit()simply raises theSystemExitexception.
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.
exit()is an alias forquit(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.
sys.exit()also raises theSystemExitexception. This means that it is the same asquitandexitin 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.
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 byos.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.
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()orquit()in the REPL.Use
sys.exit()in scripts, orraise SystemExit()if you prefer.Use
os._exit()for child processes to exit after a call toos.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.
Whats is the function of exit()?
Return vs sys.exit()
Exit a Python program, and Python, COMPLETELY.
How to exit Python program if an if statement is true?
Videos
Trying to get the following program to exit:
import os
import tkinter as tk
root = tk.Tk()
root.protocol("WM_DELETE_WINDOW", root.destroy)
root.geometry("600x600")
# Create canvas
canvas = tk.Canvas(root, width=600, height=600)
canvas.pack()
# Create circle
circle = canvas.create_oval(10, 10, 60, 60, fill="red")
# Animation control variables
count = 0
# Animate function
def animate():
global count
# Move circle
canvas.coords(circle, 10, 10, 60, 60)
root.after(250, lambda: canvas.coords(circle, 540, 540, 590, 590))
root.after(500, lambda: canvas.coords(circle, 490, 10, 540, 60))
root.after(750, lambda: canvas.coords(circle, 10, 490, 60, 540))
count += 1
if count < 100:
root.after(1000, animate)
# Play function
# def play():
# global count
# count = 0
# animate()
# Create play button
# button = tk.Button(root, text="Play", command=play)
# button.pack()
# Start animation
animate()
root.mainloop()
# print("Great job!")
# Now trying to get the friggin window to close....
# root.quit()
# root.destroy()
# tk.mainloop.quit()
# sys.exit()
#if window_wont_close:
#os.system("taskkill /f /im python.exe")
How to have Python exit a program provided the elif statement below is true? I tried exit(), break, and quit(), but what happened was the last thing the program said was Goodbye, and there was a blinking underscore, so the program was still running and the commands didn't work.
if numbers == 1:
answer = input(f"How many?")
elif numbers == 0:
answer = f"Exiting program..."
print("Goodbye!")
exit()
else:
answer = input(f"Let's go back.")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)