views:

187

answers:

2
+2  Q: 

exit from ipython

I like IPython a lot for working with the python interpreter. However, I continually find myself typing exit to exit, and get prompted "Type exit() to exit."

I know I can type Ctrl-D to exit, but is there a way I can type exit without parentheses and get IPython to exit?

Update: Thanks to nosklo, this can be easily done by adding the following line to the main() function in your ipy_user_conf.py:

# type exit to exit
ip.ex("type(exit).__repr__ = lambda s: setattr(s.shell, 'exit_now', True) or ''")
+3  A: 

%exit, or %Exit, if you have confirmation enabled and want to skip it. You can alias it to e.g. %e by putting execute __IPYTHON__.magic_e = __IPYTHON__.magic_exit in your ipythonrc.

PiotrLegnica
Thanks, but I really want to literally type "exit". Because it is stuck in muscle memory.
Jason Sundram
+6  A: 
>>> import sys
>>> class Quitter(object):
...     def __repr__(self):
...         sys.exit()
... 
>>> exit = Quitter()

You can use it like this:

>>> exit

EDIT:

I dont use ipython myself, but it seems to have some wierd sys.exit handler. The solution I found is as follows:

In [1]: type(exit).__repr__ = lambda s: setattr(s.shell, 'exit_now', True) or ''

Usage:

In [2]: exit
nosklo
This doesn't quite work with IPython. It has its own Quitter class which calls the appropriate exit method on the actual IPShell instance. But the general idea is correct.
Robert Kern
@Robert Kern: Okay, found an `ipython` solution.
nosklo
@nosklo -- Thanks, that's awesome.
Jason Sundram