重置 ipython 内核

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26603456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:44:49  来源:igfitidea点击:

Reset ipython kernel

pythonkernelipythonreload

提问by greole

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 %resetbut that doesn't seem to clear the imports.

我想知道是否有一种方法可以在不关闭的情况下重新启动 ipython 内核,就像笔记本中存在的内核重新启动功能一样。我试过了,%reset但这似乎并没有清除进口。

回答by greole

Even though it would be handy if %resetwould clear the namespace and the cache for the imports (as in the notebook) one can explicitly reload a previously imported module using importlib.reloadin python3.4 or imp.reloadin python3.0-3.3 (and if needed reset the kernel in a second step).

尽管如果%reset清除导入的命名空间和缓存(如在笔记本中)会很方便,但可以使用importlib.reloadpython3.4 或imp.reloadpython3.0-3.3显式重新加载以前导入的模块(并且如果需要重置内核在第二步)。

回答by Seekheart

In the qt console you could hit ctrl-

在 qt 控制台中,您可以点击 ctrl-

回答by Nizam Mohamed

I could restart the kernel, but some console sessions take longer to reconnect. Notebook detects kernel restart instantly.

我可以重新启动内核,但某些控制台会话需要更长的时间才能重新连接。Notebook 立即检测到内核重新启动。

ipykernel.ipkernel.IPythonKernelclass has a do_shutdownmethod with restartparameter which defaults to False.

ipykernel.ipkernel.IPythonKernel类有一个do_shutdownrestart参数的方法,默认为False.

Get a reference to ipykernel.kernelapp.IPKernelAppwhich has a reference to the kernel and call do_shutdownof the kernel by passing True.

得到一个参考ipykernel.kernelapp.IPKernelApp具有内核和呼叫参考do_shutdown通过给内核的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

回答by gokul_uf

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.

IPython Qt 控制台具有重置内核功能。如果您使用的是 IPython Qt,则可以使用它。IMO 比从 shell 使用要好。

enter image description here

在此处输入图片说明

回答by Jayesh Baviskar

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+. which is a shortcut key to restart the console.

或者您可以使用 CTRL+。这是重启控制台的快捷键。

回答by MSIS

In 3.7 Anaconda, just go to Kernel, select Restart

在 3.7 Anaconda 中,只需转到 Kernel,选择 Restart

enter image description here

在此处输入图片说明

回答by Nielsou Akbrg

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 文件中的代码,而无需重新启动内核,这对我来说是一个巨大的痛点。