149

I've seen people suggesting sys.exit() in Python. My question is that, is there any other way to exit the execution of current script, I mean termination, with an error.

Something like this:

sys.exit("You can not have three process at the same time.")

Currently my solution would be:

print("You can not have three process at the same time.")
sys.exit()
1

5 Answers 5

178

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

Sign up to request clarification or add additional context in comments.

1 Comment

Just higligting one important point of using sys.exit("some error message") from the python documentaion. If another type of object is passed, None is equivalent to passing zero, and any other object is printed to stderr and results in an exit code of 1
73

There are 3 approaches, the first as lvc mentioned is using sys.exit

sys.exit('My error message')

The second way is using print, print can write almost anything including an error message

print >>sys.stderr, "fatal error"     # Python 2.x
print("fatal error", file=sys.stderr) # Python 3.x

The third way is to rise an exception which I don't like because it can be try-catch

  raise SystemExit('error in code want to exit')

it can be ignored like this

try:
  raise SystemExit('error in code want to exit')
except:
  print("program is still open")

3 Comments

Just an FYI, sys.exit() raises a SystemExit exception. Hence it can also be caught in a try-except block.
@hr87 I said it if you read carefully
@nimamoradi actually you should be the one reading carefully... hr87 is saying that sys.exit() also raises an exception under the hood, making your third point (not liking raise SystemExit because it can be try-catched) moot.
29

You can also raise an error like this:

raise SystemExit('Error: 3 processes cannot run simultaneously.')

One advantage of this approach is that you don't have to import the Python sys module. This works on Linux with Python 3 and Python 2. I have not tested it on Windows or Mac OS.

2 Comments

Works for me on macos w pyenv 2 & 3
Works on Windows 10
8

You have to use import sys first

Then use sys.exit("your custom error message")

Comments

3

There's also a convenience function called exit() in Python, no need to import anything for this.

Here's a simple example:

$ python3 -c "exit('This should work...')" ; echo "exit code: $?"
This should work...
exit code: 1

2 Comments

I get name 'exit' is not defined (Python 3.11)
@rhody I'm sorry to hear that, and not sure why. I've just tried to run the snippet in a terminal on a fresh install of Ubuntu 22.04 using Python 3.10.12, on my main Ubuntu 22.04 box using Python 3.11.7 & Python 3.10.12, also on macOS (M1) using Python 3.11.5 & Python 3.10.13; it worked in each case. From a quick Google search, I found there's no simple answer to why this happens to some. I figured it's safer to use either import sys; sys.exit() or raise SystemExit().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.