Showing results for 
Search instead for 
Did you mean: 

Is there a graceful way to cancel a Python script?



as the title says, I'm wondering if there is a graceful way to exit a python script.

Calling sys.exit / raising SystemExit always seems to trigger an error message, independently of the exit code that is passed. (I tried it with 0, 1, -1, None)

0 Kudos
Message 1 of 3

If I understood it correctly, I think the best way is to use the return command.


def your_function():
    # Insert your code here
    # .....
    # .....



0 Kudos
Message 2 of 3

Hello panta1978,

unfortunately that can only be used within functions.

And execution will continue where the function was called. It will not exit the script.


When used outside of a function it is considered a syntax error.

The script won't even start.

0 Kudos
Message 3 of 3