[英]Reset ipython kernel
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.我想知道是否有一种方法可以在不关闭的情况下重新启动 ipython 内核,就像笔记本中存在的内核重新启动功能一样。 I tried %reset
but that doesn't seem to clear the imports.我试过%reset
但这似乎并没有清除进口。
尽管如果%reset
会清除导入的命名空间和缓存(如笔记本中)会很方便,但可以使用importlib.reload
中的 importlib.reload 或imp.reload
中的 imp.reload 显式重新加载以前导入的模块- 3.3(如果需要,在第二步重置内核)。
I could restart the kernel, but some console sessions take longer to reconnect.我可以重新启动内核,但某些控制台会话需要更长的时间才能重新连接。 Notebook detects kernel restart instantly. Notebook 立即检测到内核重新启动。
ipykernel.ipkernel.IPythonKernel
class has a do_shutdown
method with restart
parameter which defaults to False
. ipykernel.ipkernel.IPythonKernel
类有一个带有restart
参数的do_shutdown
方法,默认为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
.获取对ipykernel.kernelapp.IPKernelApp
的引用,它引用内核并通过传递True
调用内核的do_shutdown
。
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.如果你已经用 anaconda 安装了 Spyder,那么打开 Spyder 窗口。
Then Consoles (menu bar) -> Restart Consoles.然后控制台(菜单栏)-> 重新启动控制台。
or you can use CTRL+.或者您可以使用 CTRL+。 which is a shortcut key to restart the console.这是重启控制台的快捷键。
I personaly use add these two lines at the top of each ipynb file in JupyterLab:我个人使用在 JupyterLab 中每个 ipynb 文件的顶部添加这两行:
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.它允许您更新相邻 xxx.py 文件中的代码,而无需重新启动内核,这对我来说是一个巨大的痛点。
在 qt 控制台中,您可以按ctrl-
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.