解决Jupyter notebook找不到内核的问题

文章讲述了在启动JupyterNotebook时遇到内核错误的具体Traceback,问题根源在于kernel.json文件中的Python解释器路径不正确。解决方案包括修改kernel.json文件中的Python路径,确保使用正确的双反斜杠格式,更新IPython,以及重新安装Python内核。
摘要由CSDN通过智能技术生成

新建一个Jupyter notebook,发现内核显示error,报错的详情如下:

Traceback (most recent call last):
File "D:\software\anaconda\Lib\site-packages\tornado\web.py", line 1713, in _execute
result = await result
^^^^^^^^^^^^
File "D:\software\anaconda\Lib\asyncio\tasks.py", line 339, in __wakeup
future.result()
File "D:\software\anaconda\Lib\site-packages\tornado\gen.py", line 776, in run
yielded = self.gen.throw(*exc_info) # type: ignore
^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\software\anaconda\Lib\site-packages\notebook\services\sessions\handlers.py", line 74, in post
model = yield maybe_future(
^^^^^^^^^^^^^^^^^^^
File "D:\software\anaconda\Lib\site-packages\tornado\gen.py", line 769, in run
value = future.result()
^^^^^^^^^^^^^^^
File "D:\software\anaconda\Lib\site-packages\tornado\gen.py", line 776, in run
yielded = self.gen.throw(*exc_info) # type: ignore
^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\software\anaconda\Lib\site-packages\notebook\services\sessions\sessionmanager.py", line 98, in create_session
kernel_id = yield self.start_kernel_for_session(session_id, path, name, type, kernel_name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\software\anaconda\Lib\site-packages\tornado\gen.py", line 769, in run
value = future.result()
^^^^^^^^^^^^^^^
File "D:\software\anaconda\Lib\site-packages\tornado\gen.py", line 776, in run
yielded = self.gen.throw(*exc_info) # type: ignore
^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\software\anaconda\Lib\site-packages\notebook\services\sessions\sessionmanager.py", line 110, in start_kernel_for_session
kernel_id = yield maybe_future(
^^^^^^^^^^^^^^^^^^^
File "D:\software\anaconda\Lib\site-packages\tornado\gen.py", line 769, in run
value = future.result()
^^^^^^^^^^^^^^^
File "D:\software\anaconda\Lib\asyncio\futures.py", line 203, in result
raise self._exception.with_traceback(self._exception_tb)
File "D:\software\anaconda\Lib\asyncio\tasks.py", line 267, in _step
result = coro.send(None)
^^^^^^^^^^^^^^^
File "D:\software\anaconda\Lib\site-packages\notebook\services\kernels\kernelmanager.py", line 176, in start_kernel
kernel_id = await maybe_future(self.pinned_superclass.start_kernel(self, **kwargs))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\software\anaconda\Lib\site-packages\jupyter_core\utils_init.py", line 158, in wrapped
return runner_map[name].run(inner)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\software\anaconda\Lib\site-packages\jupyter_core\utils_init.py", line 125, in run
return fut.result(None)
^^^^^^^^^^^^^^^^
File "D:\software\anaconda\Lib\concurrent\futures_base.py", line 456, in result
return self.__get_result()
^^^^^^^^^^^^^^^^^^^
File "D:\software\anaconda\Lib\concurrent\futures_base.py", line 401, in __get_result
raise self._exception
File "D:\software\anaconda\Lib\asyncio\tasks.py", line 267, in __step
result = coro.send(None)
^^^^^^^^^^^^^^^
File "D:\software\anaconda\Lib\site-packages\jupyter_client\multikernelmanager.py", line 207, in async_start_kernel
starter = ensure_async(km.start_kernel(**kwargs))
^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\software\anaconda\Lib\site-packages\jupyter_core\utils_init.py", line 158, in wrapped
return runner_map[name].run(inner)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\software\anaconda\Lib\site-packages\jupyter_core\utils_init.py", line 125, in run
return fut.result(None)
^^^^^^^^^^^^^^^^
File "D:\software\anaconda\Lib\concurrent\futures_base.py", line 456, in result
return self.__get_result()
^^^^^^^^^^^^^^^^^^^
File "D:\software\anaconda\Lib\concurrent\futures_base.py", line 401, in __get_result
raise self._exception
File "D:\software\anaconda\Lib\asyncio\tasks.py", line 267, in __step
result = coro.send(None)
^^^^^^^^^^^^^^^
File "D:\software\anaconda\Lib\site-packages\jupyter_client\manager.py", line 93, in wrapper
raise e
File "D:\software\anaconda\Lib\site-packages\jupyter_client\manager.py", line 85, in wrapper
out = await method(self, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\software\anaconda\Lib\site-packages\jupyter_client\manager.py", line 401, in _async_start_kernel
await self._async_launch_kernel(kernel_cmd, **kw)
File "D:\software\anaconda\Lib\site-packages\jupyter_client\manager.py", line 317, in _async_launch_kernel
connection_info = await self.provisioner.launch_kernel(kernel_cmd, **kw)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\software\anaconda\Lib\site-packages\jupyter_client\provisioning\local_provisioner.py", line 205, in launch_kernel
self.process = launch_kernel(cmd, **scrubbed_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\software\anaconda\Lib\site-packages\jupyter_client\launcher.py", line 167, in launch_kernel
raise ex
File "D:\software\anaconda\Lib\site-packages\jupyter_client\launcher.py", line 155, in launch_kernel
proc = Popen(cmd, **kwargs)
^^^^^^^^^^^^^^^^^^^^
File "D:\software\anaconda\Lib\subprocess.py", line 1026, in init
self._execute_child(args, executable, preexec_fn, close_fds,
File "D:\software\anaconda\Lib\subprocess.py", line 1538, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [WinError 2] 系统找不到指定的文件。

是不是头都麻了?

要解决这个问题,请按照以下步骤:

1.  访问C:\Users\ASUS\AppData\Roaming\jupyter\kernels\python3,找到kernel.json文件,将文件中的内容改为:

{
 "argv": [
  "C:\\Users\\ASUS\\AppData\\Local\\Programs\\Python\\Python38\\python.exe",
  "-m",
  "ipykernel_launcher",
  "-f",
  "{connection_file}"
 ],
 "display_name": "Python (base)",
 "language": "python",
 "metadata": {
  "debugger": true
 }
}

请注意,在 Windows 系统中,路径中的反斜杠 (\) 在 JSON 文件中需要被转义,即使用两个反斜杠 (\\)。在您的原始 kernel.json 文件中,路径被写作 "C:\Users\ASUS\AppData\Local\Programs\Python\Python38\python.exe",而正确的写法应为 "C:\\Users\\ASUS\\AppData\\Local\\Programs\\Python\\Python38\\python.exe"

2.  更新IPython

pip install --upgrade jupyter ipython

3.  安装注册Python内核

python -m ipykernel install --user

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值