ipython: exit() statement not working as expected

Viewed 2635

I am running a script into ipython (1.2.1) and I need it to stop if a certain condition is not met. I tried to use the exit() statement, but it is not behaving as expected.

Take for example the following script which I called test.py:

if(True):
    print('Error')
    exit()
print('Still here!')

When I run it using python test.py, I get:

$python test.py
Error

And then the execution is terminated, as expected.

But if I run it from ipython using run -i test.py, then I get:

In [1]: run -i test.py
Error
Still here!

And finally the execution of ipython is terminated. The problem is that in this case the second print statement is still executed, while I would need the execution of the script to be terminated as soon as the exit() statement is encountered.

Why is this happening and how can I obtain the result I want? (I am running python 2.7.6)

1 Answers
Related