Reset ipython kernel

血红的双手。 提交于 2019-11-29 05:25:32

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

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

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.

Seekheart

In the qt console you could hit ctrl-

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!