16

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 ''") 
1
  • 2
    I also have (had) your problems using 'exit' because of the same muscle memory. For anyone new to IPython (or looking for a quicker way), hitting <ctrl>-D twice is faster. Just don't hit it three times or you'll be logged out of your (linux) shell as well. Commented Sep 24, 2010 at 18:54

5 Answers 5

16
>>> 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
Sign up to request clarification or add additional context in comments.

2 Comments

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: Okay, found an ipython solution.
5

%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.

3 Comments

Thanks, but I really want to literally type "exit". Because it is stuck in muscle memory.
%exit or %Exit are no line magic functions under IPython version 1.1.0. So using IPython 1.1.0 this answer does not work for me at all.
@halloleo My heart bleeds for you on the account of answer from 5 years ago no longer being relevant. exit works OOB on recent IPython versions and this entire question is unnecessary.
2

With iPython 6.2.1, you can simply type exit to exit the interpreter:

$ ipython
Python 3.5.2 (default, Nov 23 2017, 16:37:01) 
Type 'copyright', 'credits' or 'license' for more information
IPython 6.2.1 -- An enhanced Interactive Python. Type '?' for help.
In [1]: exit
$ 

Comments

0

At least in IPython 0.10, you should be able to set IPYTHON.rc.confirm_exit = False

Comments

0

On Linux, I find myself constantly using ctrl-d to exit from every type REPL interface, including ipython. There is need to type exit/logout almost always.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.