1# There are 3 approaches, the first as lvc mentioned is using sys.exit
2sys.exit('My error message')
3
4# The second way is using print, print can write almost anything including an error message
5print >>sys.stderr, "fatal error" # Python 2.x
6print("fatal error", file=sys.stderr) # Python 3.x
7
8# The third way is to rise an exception which I don't like because it can be try-catch
9raise SystemExit('error in code want to exit')
10
11# it can be ignored like this
12try:
13 raise SystemExit('error in code want to exit')
14except:
15 print("program is still open")