问题
在Jupyter notebook中,如何解决找不到Python解释器的问题。
环境
- Ubuntu 18.04
- 使用 Python 3.7 的 Anaconda 环境
问题
启动一个jupyter notebook,用Python 3内核创建一个notebook,报错。 nlp_in_tensorflow 是一个已删除的 conda 环境。
Traceback (most recent call last):
File "/home/user/conda/envs/cs231n/lib/python3.7/site-packages/tornado/web.py", line 1704, in _execute
result = await result
File "/home/user/conda/envs/cs231n/lib/python3.7/site-packages/tornado/gen.py", line 769, in run
yielded = self.gen.throw(*exc_info) # type: ignore
File "/home/user/conda/envs/cs231n/lib/python3.7/site-packages/notebook/services/sessions/handlers.py", line 72, in post
type=mtype))
File "/home/user/conda/envs/cs231n/lib/python3.7/site-packages/tornado/gen.py", line 762, in run
value = future.result()
File "/home/user/conda/envs/cs231n/lib/python3.7/site-packages/tornado/gen.py", line 769, in run
yielded = self.gen.throw(*exc_info) # type: ignore
File "/home/user/conda/envs/cs231n/lib/python3.7/site-packages/notebook/services/sessions/sessionmanager.py", line 88, in create_session
kernel_id = yield self.start_kernel_for_session(session_id, path, name, type, kernel_name)
File "/home/user/conda/envs/cs231n/lib/python3.7/site-packages/tornado/gen.py", line 762, in run
value = future.result()
File "/home/user/conda/envs/cs231n/lib/python3.7/site-packages/tornado/gen.py", line 769, in run
yielded = self.gen.throw(*exc_info) # type: ignore
File "/home/user/conda/envs/cs231n/lib/python3.7/site-packages/notebook/services/sessions/sessionmanager.py", line 101, in start_kernel_for_session
self.kernel_manager.start_kernel(path=kernel_path, kernel_name=kernel_name)
File "/home/user/conda/envs/cs231n/lib/python3.7/site-packages/tornado/gen.py", line 762, in run
value = future.result()
File "/home/user/conda/envs/cs231n/lib/python3.7/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 "/home/user/conda/envs/cs231n/lib/python3.7/site-packages/jupyter_client/multikernelmanager.py", line 185, in start_kernel
km.start_kernel(**kwargs)
File "/home/user/conda/envs/cs231n/lib/python3.7/site-packages/jupyter_client/manager.py", line 313, in start_kernel
self.kernel = self._launch_kernel(kernel_cmd, **kw)
File "/home/user/conda/envs/cs231n/lib/python3.7/site-packages/jupyter_client/manager.py", line 220, in _launch_kernel
return launch_kernel(kernel_cmd, **kw)
File "/home/user/conda/envs/cs231n/lib/python3.7/site-packages/jupyter_client/launcher.py", line 131, in launch_kernel
proc = Popen(cmd, **kwargs)
File "/home/user/conda/envs/cs231n/lib/python3.7/subprocess.py", line 800, in __init__
restore_signals, start_new_session)
File "/home/user/conda/envs/cs231n/lib/python3.7/subprocess.py", line 1551, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: '/home/user/conda/envs/nlp_in_tensorflow/bin/python3': '/home/user/conda/envs/nlp_in_tensorflow/bin/python3'
最佳答案
原因
Python 3 内核的 kernel.json 文件指向已删除的环境。
$ jupyter kernelspec list
Available kernels:
python3 /home/oonisim/.local/share/jupyter/kernels/python3
$ cat ~/.local/share/jupyter/kernels/python3/kernel.json
{
"argv": [
"/home/user/conda/envs/nlp_in_tensorflow/bin/python3", <----- Referring to the deleted environment
"-m",
"ipykernel_launcher",
"-f",
"{connection_file}"
],
"display_name": "Python 3",
"language": "python"
}
资源
Multiple python environments, whether based on Anaconda or Python Virtual environments, are often the source of reported issues. In many cases, these issues stem from the Notebook server running in one environment, while the kernel and/or its resources, derive from another environment.
Another thing to check is the kernel.json file that will be located in the aforementioned kernel specs directory identified by running jupyter kernelspec list. This file will contain an argv stanza that includes the actual command to run when launching the kernel. Oftentimes, when reinstalling python environments, a previous kernel.json will reference an python executable from an old or non-existent location. As a result, it’s always a good idea when encountering kernel startup issues to validate the argv stanza to ensure all file references exist and are appropriate.
修复
删除了 ~/.local/share/jupyter/kernels/python3/kernel.json。
相关问题
- Jupyter Notebook is loading incorrect Python kernel #2563
- Running Jupyter with multiple Python and IPython paths - 3. How Jupyter knows what Python to use
Jupyter is set-up to be able to use a wide range of "kernels", or execution engines for the code. These can be Python 2, Python 3, R, Julia, Ruby... there are dozens of possible kernels to use. But in order for this to happen, Jupyter needs to know where to look for the associated executable: that is, it needs to know which path the python sits in.
These paths are specified in jupyter's kernelspec, and it's possible for the user to adjust them to their desires. For example, here's the list of kernels that I have on my system:
关于python-3.x - Jupyter 笔记本 Python 内核 - FileNotFoundError : [Errno 2] No such file or directory python3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65300509/