为什么JupyterHub内核与kernel.json不对齐?

时间:2018-07-09 22:48:33

标签: docker jupyter-notebook

我正在使用DockerSpawner的SystemUserSpawner(https://github.com/jupyterhub/dockerspawner)生成Docker容器以在其中运行笔记本。

这些容器中的Jupyter在Python 3上运行,现在我正在尝试向其中添加Python 2内核。

RUN pip3.6 --no-cache install \
    jupyterhub==0.9.0 \
    'notebook>=5.0,<=6.0'
...
RUN python2 -m pip install --upgrade pip
RUN python2 -m pip --no-cache install 'ipython<6'
RUN python2 -m pip --no-cache install ipykernel

此后,Python 2内核确实出现在Jupyter的列表中(如预期的那样),但是当我启动它并在“ Python 2”笔记本中运行以下命令时,它实际上正在其中运行Python 3验证“真实”版本:

import sys
print(sys.version)

3.6.5 (default, Apr 10 2018, 17:08:37) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)]

查看此容器中的可用内核,一切似乎都很好:

[zoltan.fedor@7a30bb98f7c0 ~]$ jupyter kernelspec list
Available kernels:
  python2    /usr/share/jupyter/kernels/python2
  python3    /usr/share/jupyter/kernels/python3

但是实际的内核文件是相同的(标题除外):

vi /usr/share/jupyter/kernels/python2/kernel.json
-----
{
 "display_name": "Python 2",
 "language": "python",
 "argv": [
  "python",
  "-m",
  "ipykernel_launcher",
  "-f",
  "{connection_file}"
 ]
}

Python 3:

vi /usr/share/jupyter/kernels/python3/kernel.json
----
{
 "argv": [
  "python",
  "-m",
  "ipykernel_launcher",
  "-f",
  "{connection_file}"
 ],
 "display_name": "Python 3",
 "language": "python"
}

只需运行“ python”,即可启动Python 2-

[zoltan.fedor@7a30bb98f7c0 ~]$ python
Python 2.7.5 (default, Apr 11 2018, 07:36:10) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

那么为什么这两个内核都在其中运行Python 3?如果有的话,那么我会看到它们都运行python 2,因为它们使用“ python”作为“ argv”,应以python 2.7.5而不是3.6.5开头。

知道我在这里缺少什么吗?

1 个答案:

答案 0 :(得分:1)

事实证明,问题仅仅是我忘记运行以下内容。

RUN python2 -m ipykernel install

奇怪的是,即使没有此命令也创建了python2 kernel.json,只是未将其设置为执行python 2。 之后,python2内核现在指向:

$ jupyter kernelspec list
Available kernels:
  python2    /usr/local/share/jupyter/kernels/python2
  python3    /usr/share/jupyter/kernels/python3

kernel.json的内容为:

$ vi /usr/local/share/jupyter/kernels/python2/kernel.json
----
{
 "display_name": "Python 2",
 "language": "python",
 "argv": [
  "/usr/bin/python2",
  "-m",
  "ipykernel_launcher",
  "-f",
  "{connection_file}"
 ]
}

现在这确实是python 2内核。