I think you can use

sys.exit(0)

You may check it here in the python 2.7 doc:

The optional argument arg can be an integer giving the exit status (defaulting to zero), or another type of object. If it is an integer, zero is considered “successful termination” and any nonzero value is considered “abnormal termination” by shells and the like.

Answer from godidier on Stack Overflow
🌐
Python
docs.python.org › 3 › library › sys.html
sys — System-specific parameters and functions
If Python is unable to retrieve the real path to its executable, sys.executable will be an empty string or None. ... Raise a SystemExit exception, signaling an intention to exit the interpreter.
Discussions

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
0
0
February 5, 2024
Python won't spit out sys.exit message in try function
Mike Hansen is having issues with: Hey! I'm currently playing around with Python trying to learn the language and I decided to take the end project, making a tickets app in cmd pr... More on teamtreehouse.com
🌐 teamtreehouse.com
2
March 31, 2020
Is it better to quit a script due to a user input error using sys.exit(1) or raise Error()?
I think that's just a style decision; I don't think there is a best practice for that. Personally I'd use the raise option, probably because I like to think people use my scripts in their scripts, and the raise option allows them to catch it easier. More on reddit.com
🌐 r/learnpython
6
1
December 9, 2022
Qwen3.5-397B-A17B run in dual spark! but I have a concern
Intel released Qwen3.5-397B-A17B-int4-AutoRound on Hugging Face, and it runs well with vLLM when setting max-model-len to 262144. It outputs an average of 26 tok/s based on a single request, while Qwen3.5-122B-A10B-FP8 seems to output around 31 tok/s. Since it won’t be used by just one person ... More on forums.developer.nvidia.com
🌐 forums.developer.nvidia.com
0
4
1 month ago
🌐
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)

🌐
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 - And it stop a program in Python. ... import sys age = 17 if age < 18: sys.exit("Age less than 18") else: print("Age is not less than 18")
🌐
Python.org
discuss.python.org › python help
Return vs sys.exit() - Python Help - Discussions on Python.org
February 5, 2024 - 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 ...
🌐
Python
docs.python.org › 3 › library › subprocess.html
subprocess — Subprocess management
All of the functions and methods that accept a timeout parameter, such as run() and Popen.communicate() will raise TimeoutExpired if the timeout expires before the process exits. Exceptions defined in this module all inherit from SubprocessError. Added in version 3.3: The SubprocessError base class was added. Unlike some other popen functions, this library will not implicitly choose to call a system shell.
Find elsewhere
🌐
Python Pool
pythonpool.com › home › blog › why is python sys.exit better than other exit functions?
Why is Python sys.exit better than other exit functions? - Python Pool
July 10, 2021 - Everything else is the same about the two methods except that, for sys.exit, you have to import sys. Two more ways are exit() and quit().
Top answer
1 of 2
1
I wouldn't use a try block there. There is no error for it to fail on. You could use a a try block to stop them from entering letters or for a divide by 0. I would just use an else like this: ```python import sys import math price in $ TICKET_PRICE = 10 tickets_remaining = 100 Asking for username through input and assigning it to a new variable name = input("Hello! What is your name? ") Saying hello to the user, including name, making it more personal print("Welcome to the page, {}. There are currently {} tickets remaining.".format(name, tickets_remaining)) Using the try/except function here as I want to prevent users from buying more than what's actually in stock. If the user exceeds the ticket_remaining they will be thrown out, else, continue try: quantity = int(input("How many tickets would you like to buy? ")) if quantity > ticketsremaining: sys.exit("Woah! Slow down there, Cowboy! You're trying to buy too many tickets. We only have {} ticket(s) left.".format(ticketsremaining)) else: #except: subtotal = math.ceil(quantity * TICKET_PRICE) answer = input("The subtotal is gonna be ${}, would you like to continue? (yes/no) ".format(subtotal)) if answer == 'yes': sys.exit(("Sold to the buyer {} for ${}! Thanks for the purchase, {}, we hope that you enjoy the show.".format(name, subtotal, name))) else: sys.exit("Okay, then.") ``` I think it also asks to make a loop to buy multiple tickets. Hopefully you will give that a try too. Good luck and keep at it!
2 of 2
0
I'm not sure how you were seeing that final message. According to the documentation: The sys.exit() function allows the developer to exit from Python. The exit function takes an optional argument, typically an integer, that gives an exit status. Zero is considered a “successful termination”.
🌐
Reddit
reddit.com › r/learnpython › is it better to quit a script due to a user input error using sys.exit(1) or raise error()?
r/learnpython on Reddit: Is it better to quit a script due to a user input error using sys.exit(1) or raise Error()?
December 9, 2022 -

Let's say you have a script that is a simple csv parser but the user enters an invalid filepath for the csv.

Is it better to exit the script with a print("wrong filepath") and then sys.exit(1) or by using raise SomeError("wrong filepath")?

From what I read sys.exit(1) raises an exception also, but it doesn't seem to print a traceback like using raise SomeError("wrong filepath") does. So you can just print the message you want the user to see and exit quietly.

If I were to distribute a script like this to public users. What's the better practice?

🌐
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”.
🌐
NVIDIA Developer Forums
forums.developer.nvidia.com › accelerated computing › dgx spark / gb10 user forum › dgx spark / gb10
Qwen3.5-397B-A17B run in dual spark! but I have a concern - DGX Spark / GB10 - NVIDIA Developer Forums
1 month ago - Intel released Qwen3.5-397B-A17B-int4-AutoRound on Hugging Face, and it runs well with vLLM when setting max-model-len to 262144. It outputs an average of 26 tok/s based on a single request, while Qwen3.5-122B-A10B-FP8 seems to output around 31 tok/s. Since it won’t be used by just one person but by 2~3 people, I decided to use the vLLM serving tool.
🌐
VISIT FLORIDA
visitflorida.com
Florida Vacations, Travel & Tourism Guide | VISIT FLORIDA
Official state travel, tourism and vacation website for Florida, featuring maps, beaches, events, deals, photos, hotels, activities, attractions and other planning information.
🌐
Python Morsels
pythonmorsels.com › exiting-a-python-program
Exiting a Python program - Python Morsels
February 21, 2022 - To exit a Python program early, call sys.exit with 0 for success, 1 for error, or a string (to print an error message while exiting).
🌐
Real Python
realpython.com › ref › builtin-exceptions › systemexit
SystemExit | Python’s Built-in Exceptions – Real Python
SystemExit is a built-in exception that Python raises when the sys.exit() function is called, and it’s used to terminate the Python interpreter.
🌐
GeeksforGeeks
geeksforgeeks.org › operating systems › introduction-of-system-call
System Call - GeeksforGeeks
December 15, 2025 - Services provided by an OS are typically related to any kind of operation that a user program can perform like creation, termination, forking, moving, communication, etc. Similar types of operations are grouped into one single system call category.
🌐
Google
google.github.io › styleguide › cppguide.html
Google C++ Style Guide
POSIX, Linux, and Windows system headers (e.g., <unistd.h> and <windows.h>). In rare cases, third_party libraries (e.g., <Python.h>).
🌐
Ubuntu
ubuntu.com › desktop
Ubuntu Desktop PC operating system | Ubuntu
The number 1 open source operating system powering millions of PCs and laptops around the world.
🌐
Adam Johnson
adamj.eu › tech › 2021 › 10 › 10 › the-many-ways-to-exit-in-python
The Many Ways to Exit in Python - Adam Johnson
October 10, 2021 - If you’re looking for a quick answer, you can stop reading here. Use raise SystemExit(<code>) as the obviousest way to exit from Python code and carry on with your life.
🌐
Python documentation
docs.python.org › 3 › reference › datamodel.html
3. Data model — Python 3.14.3 documentation
For implicitly created tracebacks, when the search for an exception handler unwinds the execution stack, at each unwound level a traceback object is inserted in front of the current traceback. When an exception handler is entered, the stack trace is made available to the program. (See section The try statement.) It is accessible as the third item of the tuple returned by sys.exc_info(), and as the __traceback__ attribute of the caught exception.