[IPython-user] EOF for the cmd class

Fernando Perez Fernando.Perez at colorado.edu
Thu Nov 25 01:13:28 CST 2004


Justin Walker wrote:

> If I run this code and type ^D in python, the interpreter exits to 
> shell.  If I do the same with ipython, it complains, with a traceback, 
> and the comment that that I should use ^D to exit (:-}).

Ipython is behaving as intended in this case: when you type 'run rolo', 
ipython will execute your python script, but it will trap all SystemExit calls 
to avoid user code blowing up ipython itself.

This is completely deliberate: it is quite common for random user code to make 
sys.exit() calls. The last thing you want, after you've been running an 
ipython session for 4 hours with 500 variables stored in memory, is to find 
yourself dumped out into the system shell because some random library you were 
using decided to call sys.exit()!

You have to think of ipython as an _interactive_ environment to work in, it is 
not a replacement to the python C interpreter (in fact, it runs on top of 
CPython).  If all you want to do is run a standalone python program, then you 
can simply run it in the shell, without ipython.

Having clarified that, let me make a suggestion: don't use sys.exit() to abort 
execution as a normal mechanism.  If you simply let your 'main' function 
return cleanly at the end of the script, python will close the execution loop, 
and ipython won't give you the nasty warnings which are now bothering you. 
You should consider sys.exit() as an emergency bailout, not as the normal 
termination mechanism.

For example, if you look at ipython itself (arguably a good example of a 
pretty complicated interactive program), it does _not_ use sys.exit() for 
termination, it simply returns from its mainloop.  It will only call 
sys.exit() if a special flag is given, for users who want to embed it in their 
normal python shells and want it to forcefully terminate the enclosing 
interpreter.

I hope this helps.

Cheers,

f




More information about the IPython-user mailing list