由于未找到文件 /run/user/*/jupyter 导致 Jupyter 笔记本内核错误

由于未找到文件 /run/user/*/jupyter 导致 Jupyter 笔记本内核错误

我在 Fedora 27 服务器上运行 jupyter 4.4.0 笔记本服务 (jupyter notebook --no-browser --port 8889),通过浏览器远程访问。使用该服务几个小时后,没有任何问题,然后出现“内核错误”。在最初的几个小时内,我可以启动新的笔记本而不会出现错误。但是一段时间后,我收到以下错误消息。它清楚地告诉我内核错误是由于缺少文件“/run/user/1001/jupyter/kernel-869f88b2-0895-40fb-b759-c518a1686484.json”造成的。

经过进一步调查,我注意到文件丢失是因为 /run/user/1001 中缺少整个 jupyter 目录。jupyter 目录似乎是在我第一次启动服务时创建的,然后似乎在一段时间后自动被删除。

经过几天的搜索,结果表明该问题可能是由于不活动造成的,导致操作系统或 jupyter 服务删除 /run/user/1001/jupyter 目录。

到目前为止,我还无法弄清楚究竟是什么删除了 /run/user/1001/jupyter 目录,或者如何阻止这种情况发生。您知道如何解决这个问题吗?

错误信息

Traceback (most recent call last):
  File "/opt/anaconda3/lib/python3.6/site-packages/tornado/web.py", line 1469, in _execute
    result = yield result
  File "/opt/anaconda3/lib/python3.6/site-packages/tornado/gen.py", line 1015, in run
    value = future.result()
  File "/opt/anaconda3/lib/python3.6/site-packages/tornado/concurrent.py", line 237, in result
    raise_exc_info(self._exc_info)
  File "<string>", line 3, in raise_exc_info
  File "/opt/anaconda3/lib/python3.6/site-packages/tornado/gen.py", line 1021, in run
    yielded = self.gen.throw(*exc_info)
  File "/opt/anaconda3/lib/python3.6/site-packages/notebook/services/sessions/handlers.py", line 73, in post
    type=mtype))
  File "/opt/anaconda3/lib/python3.6/site-packages/tornado/gen.py", line 1015, in run
    value = future.result()
  File "/opt/anaconda3/lib/python3.6/site-packages/tornado/concurrent.py", line 237, in result
    raise_exc_info(self._exc_info)
  File "<string>", line 3, in raise_exc_info
  File "/opt/anaconda3/lib/python3.6/site-packages/tornado/gen.py", line 1021, in run
    yielded = self.gen.throw(*exc_info)
  File "/opt/anaconda3/lib/python3.6/site-packages/notebook/services/sessions/sessionmanager.py", line 79, in create_session
    kernel_id = yield self.start_kernel_for_session(session_id, path, name, type, kernel_name)
  File "/opt/anaconda3/lib/python3.6/site-packages/tornado/gen.py", line 1015, in run
    value = future.result()
  File "/opt/anaconda3/lib/python3.6/site-packages/tornado/concurrent.py", line 237, in result
    raise_exc_info(self._exc_info)
  File "<string>", line 3, in raise_exc_info
  File "/opt/anaconda3/lib/python3.6/site-packages/tornado/gen.py", line 1021, in run
    yielded = self.gen.throw(*exc_info)
  File "/opt/anaconda3/lib/python3.6/site-packages/notebook/services/sessions/sessionmanager.py", line 92, in start_kernel_for_session
    self.kernel_manager.start_kernel(path=kernel_path, kernel_name=kernel_name)
  File "/opt/anaconda3/lib/python3.6/site-packages/tornado/gen.py", line 1015, in run
    value = future.result()
  File "/opt/anaconda3/lib/python3.6/site-packages/tornado/concurrent.py", line 237, in result
    raise_exc_info(self._exc_info)
  File "<string>", line 3, in raise_exc_info
  File "/opt/anaconda3/lib/python3.6/site-packages/tornado/gen.py", line 285, in wrapper
    yielded = next(result)
  File "/opt/anaconda3/lib/python3.6/site-packages/notebook/services/kernels/kernelmanager.py", line 160, in start_kernel
    super(MappingKernelManager, self).start_kernel(**kwargs)
  File "/opt/anaconda3/lib/python3.6/site-packages/jupyter_client/multikernelmanager.py", line 110, in start_kernel
    km.start_kernel(**kwargs)
  File "/opt/anaconda3/lib/python3.6/site-packages/jupyter_client/manager.py", line 240, in start_kernel
    self.write_connection_file()
  File "/opt/anaconda3/lib/python3.6/site-packages/jupyter_client/connect.py", line 472, in write_connection_file
    kernel_name=self.kernel_name
  File "/opt/anaconda3/lib/python3.6/site-packages/jupyter_client/connect.py", line 137, in write_connection_file
    with open(fname, 'w') as f:
FileNotFoundError: [Errno 2] No such file or directory: '/run/user/1001/jupyter/kernel-869f88b2-0895-40fb-b759-c518a1686484.json'

谢谢

答案1

我通过设置 JUPYTER_RUNTIME_DIR 变量解决了这个问题。Jupyter 默认将运行时文件存储在 $XDG_RUNTIME_DIR/jupyter 中。设置 JUPYTER_RUNTIME_DIR 将覆盖此行为。您可以将其设置为某个不会自动清理的目录,以防止出现此错误。

这是我在启动 jupyter 之前所做的事情:

export JUPYTER_RUNTIME_DIR="path_to_my_home_directory/.jupyter_runtime"

问题已经解决!

我发现的更多细节:

https://jupyter.readthedocs.io/en/latest/projects/jupyter-directories.html#envvar-JUPYTER_RUNTIME_DIR

https://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html#variables

相关内容