Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ''") 
like image 736
Jason Sundram Avatar asked Oct 06 '09 19:10

Jason Sundram


People also ask

How do I stop IPython from running?

Once you've clicked anywhere in the IPython Console Window (again, the output window), then, once you press ctrl+c, your program will terminate. If the IPython Console doesn't have focus when you press ctrl+c, it won't know you mean to terminate the program, and so it will ignore the ctrl+c.

How do I exit the console in Jupyter?

After all of your notebooks are closed and shut down, you can end your Jupyter Notebook session by clicking on the QUIT button at the top right of the dashboard. You can now close the browswer tab for Jupyter Notebook . If desired, you can also close your terminal by typing the command exit and hitting Enter .

How do I run IPython from command prompt?

The easiest way is to run easy_install ipython[all] as an administrator (start button, type cmd , shift+right click on “cmd.exe” and select “Run as administrator”). This installs the latest stable version of IPython including the main required and optional dependencies.

What is IPython command?

IPython bridges this gap, and gives you a syntax for executing shell commands directly from within the IPython terminal. The magic happens with the exclamation point: anything appearing after ! on a line will be executed not by the Python kernel, but by the system command-line.


2 Answers

>>> 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
like image 169
nosklo Avatar answered Oct 23 '22 11:10

nosklo


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

like image 31
Cat Plus Plus Avatar answered Oct 23 '22 12:10

Cat Plus Plus