quazgar
quazgar

Reputation: 4630

How can I terminate a python program from an embedded IPython shell?

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.

Upvotes: 2

Views: 1892

Answers (1)

quazgar
quazgar

Reputation: 4630

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)_

Upvotes: 4

Related Questions