Reset ipython kernel

72,216

Solution 1

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

Solution 2

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

Solution 3

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.

Solution 4

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.

Solution 5

In the qt console you could hit ctrl-

Share:
72,216
greole
Author by

greole

Πάντα ῥεῖ

Updated on September 04, 2020

Comments

  • greole
    greole almost 4 years

    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.