Possible Duplicates:
Terminating a Python script
Terminating a Python Program
My question is how to exit out in Python main function? I have tried ‘return‘ but it gave the error SyntaxError: 'return' outside function. Can anyone help? Thanks.
if __name__ == '__main__':
try:
if condition:
(I want to exit here)
do something
finally:
do something
You can use
sys.exit()to exit from the middle of the main function.However, I would recommend not doing any logic there. Instead, put everything in a function, and call that from
__main__– then you can use return as normal.