Calling sys.exit with a string will work. The docs mention this use explicitly:

In particular, sys.exit("some error message") is a quick way to exit a program when an error occurs.

To be more specific this will also result in an exit code of 1:

any other object is printed to stderr and results in an exit code of 1

Answer from lvc on Stack Overflow
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
9
0
February 5, 2024
Is sys.exit() bad practice?
In a library that someone else uses, if you call sys.exit, you won't be popular. In your own code, it's manageable however you want to handle it More on reddit.com
🌐 r/Python
64
111
February 13, 2018
Python logging sys.exit('message') - Stack Overflow
However, I have a lot of code that was written without proper logging that simply prints an error message before exiting. ... I'd like to know if there's a way to capture those errors, format them in the same way as other errors (with timestamp) and save them in the same a error.log file used ... More on stackoverflow.com
🌐 stackoverflow.com
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
🌐
Python
docs.python.org › 3 › library › sys.html
sys — System-specific parameters and functions
Some systems have a convention ... equivalent to passing zero, and any other object is printed to stderr and results in an exit code of 1. In particular, sys.exit("some error message") is a quick way to exit a program when an error occurs....
🌐
Piccolo-orm
piccolo-orm.com › blog › understanding-sys-exit
Understanding sys.exit - Piccolo Blog
April 21, 2021 - The number passed to sys.exit is the exit code. In Unix, an exit code of 1 means something went wrong. An exit code of 0 means it was successful. On the command line, you can see the exit code of the last command using echo $?. >>> python successful_script.py >>> echo $? 0 >>> python ...
🌐
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 - Example: In the given code, the sys.exit("Age less than 18") line will terminate the Python script with a message "Age less than 18" if the variable age is less than 18. If age is 18 or greater, it will print "Age is not less than 18".
🌐
Reddit
reddit.com › r/python › is sys.exit() bad practice?
Is sys.exit() bad practice? : r/Python
February 13, 2018 - The idea is that if there is an (unhandled) exception, it exits and displays it without throwing all of the python muck to the screen. I also often add a hidden flag to instead raise the original exception so that I can better debug ... I just do something like raise SystemExit(3) when I want ...
Find elsewhere
🌐
Python Morsels
pythonmorsels.com › exiting-a-python-program
Exiting a Python program - Python Morsels
February 21, 2022 - When sys.exit is called with a string, Python prints that string and exits with the code 1. $ python3 fixme.py /home/unicorn && echo "Success" /home/unicorn is not a directory
🌐
Tutorialspoint
tutorialspoint.com › python › python_sys_exit_method.htm
Python sys.exit() method
The message will be printed to stderr − · import sys if len(sys.argv) < 2: sys.exit("No arguments provided. Exiting the program.") print("Arguments provided. Continuing the program.") No arguments provided. Exiting the program. This example prompts the user to enter a positive number. If a negative number is entered it raises a ValueError and exits the program with an error message −
🌐
Python
bugs.python.org › issue36691
Issue 36691: SystemExit & sys.exit : Allow both exit status and message - Python tracker
This issue tracker has been migrated to GitHub, and is currently read-only. For more information, see the GitHub FAQs in the Python's Developer Guide · This issue has been migrated to GitHub: https://github.com/python/cpython/issues/80872
🌐
Notes
henryleach.com › 2025 › 02 › controlling-python-exit-codes-and-shell-scripts
Controlling Python Exit Codes and Shell Scripts - Henry Leach
February 9, 2025 - #!/bin/sh eval python exit-examples.py return_code=$? if [ $return_code = 0 ]; then echo "Success!" elif [ $return_code = 1 ]; then echo "Mild panic!" elif [ $return_code = 42 ]; then echo "Other fallback" else echo "Real Failure" exit $return_code fi · Which might be useful for kicking off other processes, or not, that it's not possible to manage from Python, and would be less error prone than trying to grep messages from stdout in your shell script.
🌐
Note.nkmk.me
note.nkmk.me › home › python
How to Exit a Python Program: sys.exit() | note.nkmk.me
April 27, 2025 - $ cat data/temp/stdout.txt $ cat data/temp/stderr.txt Error message ... In the example, the cat command displays the contents of a file on UNIX systems. On Windows, you would use type instead. ... If sys.exit() is called inside a try block, a bare except clause (without specifying the exception type) will catch the SystemExit exception, preventing the program from exiting.
🌐
Super Fast Python
superfastpython.com › exit-codes-in-python
Process Exit Codes in Python – SuperFastPython
June 12, 2022 - Recall, setting a string message as an exit code will indicate an unsuccessful exit, setting an exit code of one and reporting the string message on standard error (stderr). We can update the previous example to set a string message when calling sys.exit().
🌐
Linux Hint
linuxhint.com › python-exit-codes
Python Exit Codes – Linux Hint
Consider the example below that prints a string instead of an exit code. from sys import exit print("Hi!") exit("Program terminated unexpectedly") print("Welcome to linuxhint") In this case, we are using a string literal as the arg parameter inside the function. Once we run the program: ... We can see that the program prints the message before the exit function and the one in the exit function.
🌐
Super Fast Python
superfastpython.com › home › tutorials › exit a process with sys.exit() in python
Exit a Process with sys.exit() in Python - Super Fast Python
September 11, 2022 - It then awakes, reports a message and calls sys.exit() with an exitcode of 1. The child process terminates and the main process wakes up. The status of the child process is reported indicating that it is no longer running (as expected) and that the exit code was 1, as we set when we called sys.exit().
🌐
Real Python
realpython.com › ref › builtin-exceptions › systemexit
SystemExit | Python’s Built-in Exceptions – Real Python
Exiting. In this example, you raise SystemExit to end the program with an error message if the age is invalid. ... In this tutorial, you'll get to know some of the most commonly used built-in exceptions in Python.
🌐
Abbbi
abbbi.github.io › logging
python logging messages and exit codes
Everyone knows that an application exit code should change based on the success, error or maybe warnings that happened during execution. Lately i came along some python code that was structured the following way: #!/usr/bin/python3 import sys import logging def warnme(): # something bad happens logging.warning("warning") sys.exit(2) def evil(): # something evil happens logging.error("error") sys.exit(1) def main(): logging.basicConfig( level=logging.DEBUG, ) [..]
🌐
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?