1

I have the problem that for debugging purposes I drop into an IPython shell in a loop:

for x in large_list:
  if x.looks_bad():
    import IPython
    IPython.embed()

From there I may want to terminate the parent program, because after debugging the problem cause, embed() would be called a lot of times. sys.exit(1) is caught by IPython, so I cannot use that.

quazgar
  • 4,304
  • 2
  • 29
  • 41
  • This is actually a similar question as [173278](http://stackoverflow.com/questions/173278/is-there-a-way-to-prevent-a-systemexit-exception-raised-from-sys-exit-from-bei), just from IPython instead of the unittest module. – quazgar Sep 06 '16 at 13:55

1 Answers1

3

sys.exit just raises the SystemExit exception. The following works by hard-killing the program:

import os
os._exit(1)

To easier find this in my IPython history with Ctrl-r exit (the last line will not be saved to the history), I actually wrote this line once, with a deliberate typo:

import os; os._exit(1)_
quazgar
  • 4,304
  • 2
  • 29
  • 41