I was wondering if there is a way to restart the ipython kernel without closing it, like the kernel restart function that exists in the notebook. I tried %reset
but that doesn't seem to clear the imports.
-
1For me it seems to clear also imports (ipython 2.2, macosx): In [1]: %whos Interactive namespace is empty. In [2]: import os In [3]: %whos Variable Type Data/Info ------------------------------ os module
.7/lib/python2.7/os.pyc'> In [4]: %reset Once deleted, variables cannot be recovered. Proceed (y/[n])? y In [5]: %whos Interactive namespace is empty. – bjonen Oct 28 '14 at 10:52 -
4Yes the namespace is cleared, but when re-importing it seems to read a cached version of the module, so I have to use something like http://stackoverflow.com/questions/437589/how-do-i-unload-reload-a-python-module. – greole Oct 28 '14 at 12:35
-
1Ah ok I see. You could also use http://ipython.org/ipython-doc/dev/config/extensions/autoreload.html. But I understand this is not exactly what you are looking for. – bjonen Oct 29 '14 at 08:02
-
Call/duplicate the "kernel restart function that exists in the notebook"? – ivan_pozdeev Feb 18 '16 at 10:15
-
I do it all the time. The shortcut is Ctrl-'.' [ctrl-period]. Just tested on Linux and it only works in qtconsole. – Muposat Feb 21 '16 at 03:53
7 Answers
Even though it would be handy if %reset
would clear the namespace and the cache for the imports (as in the notebook) one can explicitly reload a previously imported module using importlib.reload
in python3.4 or imp.reload
in python3.0-3.3 (and if needed reset the kernel in a second step).

- 4,523
- 5
- 29
- 49
I could restart the kernel, but some console sessions take longer to reconnect. Notebook detects kernel restart instantly.
ipykernel.ipkernel.IPythonKernel
class has a do_shutdown
method with restart
parameter which defaults to False
.
Get a reference to ipykernel.kernelapp.IPKernelApp
which has a reference to the kernel and call do_shutdown
of the kernel by passing True
.
import IPython
app = IPython.Application.instance()
app.kernel.do_shutdown(True)
How did I test?
$ #start notebook
$ jupyter notebook
$ #connect to existing kernel
$ jupyter console --existing

- 8,751
- 24
- 32
If you have installed Spyder with anaconda, then open Spyder window.
Then Consoles (menu bar) -> Restart Consoles.
or you can use CTRL+. which is a shortcut key to restart the console.

- 295
- 4
- 3
I personaly use add these two lines at the top of each ipynb file in JupyterLab:
load_ext autoreload
%autoreload 2
It allows you to update the code in an adjacent xxx.py file, without having to restart the Kernel, which was a huge painpoint for me.

- 201
- 3
- 13
IPython Qt-console has a reset kernel feature. You could use that if you are using IPython Qt. IMO it is better than using from the shell.

- 740
- 1
- 8
- 21