3

Question

In Jupyter notebook, how to solve the issue of Python interpreter not found.

Environment

  • Ubuntu 18.04
  • Anaconda environment with Python 3.7

Problem

Start a jupyter notebook and create a notebook with Python 3 kernel and get the error. nlp_in_tensorflow was a conda environment already removed.

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'
mon
  • 18,789
  • 22
  • 112
  • 205

1 Answers1

1

Cause

The kernel.json file of the Python 3 kernel was pointing to the deleted environment.

$ 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"
}

Resource

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.

Fix

Removed ~/.local/share/jupyter/kernels/python3/kernel.json.

Related issues

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:

mon
  • 18,789
  • 22
  • 112
  • 205