我的Jupyter笔记本电脑安装了python 2内核。我不明白为什么。我可能在安装的时候搞砸了。我已经安装了python 3。我怎么能把它加到木星上? 下面是默认的Jupyter使用python3 -m install Jupyter安装并在浏览器中使用Jupyter notebook打开的截图:


当前回答

添加内核意味着你想要使用Jupyter Notebook和列表中没有显示的python版本。

简单的方法-使用所需的python版本启动笔记本,假设我安装了python3.7,然后从终端(cmd)使用以下命令来运行笔记本:

python3.7 -m notebook

有时它会使用别名py, py3.7, python来代替python3.7。

其他回答

对于jupyter/ipython的最新版本:使用jupyter kernelspec

完整文档:https://ipython.readthedocs.io/en/latest/install/kernel_install.html

列出当前内核

$ jupyter kernelspec list
Available kernels:
  python2    .../Jupyter/kernels/python2
  python3    .../Jupyter/kernels/python3

在我的例子中,python3内核设置被破坏了,因为py3.5链接不再存在,取而代之的是py3.6

添加/删除内核

删除:

$ jupyter kernelspec uninstall python3

添加一个新的: 使用你想要添加的Python并指向运行jupiter的Python:

$ /path/to/kernel/env/bin/python -m ipykernel install --prefix=/path/to/jupyter/env --name 'python-my-env'

更多例子见https://ipython.readthedocs.io/en/6.5.0/install/kernel_install.html#kernels-for-different-environments

列表:

$ jupyter kernelspec list
Available kernels:
  python3    /usr/local/lib/python3.6/site-packages/ipykernel/resources
  python2    /Users/stefano/Library/Jupyter/kernels/python2

道格:https://jupyter-client.readthedocs.io/en/latest/kernels.html kernelspecs

细节

可用的内核列在Jupyter DATA DIRECTORY的Kernels文件夹下(详情请参阅http://jupyter.readthedocs.io/en/latest/projects/jupyter-directories.html)。

例如,在macosx上,应该是/Users/YOURUSERNAME/Library/Jupyter/kernels/

内核被简单地描述为内核。Json文件,例如。/用户/我/图书馆/ Jupyter /内核/ python3 / kernel.json

{
 "argv": [
  "/usr/local/opt/python3/bin/python3.5",
  "-m",
  "ipykernel",
  "-f",
  "{connection_file}"
 ],
 "language": "python",
 "display_name": "Python 3"
}

您可以使用kernelspec命令(如上所述),而不是手动操作。以前可以通过ipython使用,现在可以通过jupyter (http://ipython.readthedocs.io/en/stable/install/kernel_install.html#kernels-for-different-environments - https://jupyter-client.readthedocs.io/en/latest/kernels.html#kernelspecs)使用。

$ jupyter kernelspec help
Manage Jupyter kernel specifications.

Subcommands
-----------

Subcommands are launched as `jupyter kernelspec cmd [args]`. For information on
using subcommand 'cmd', do: `jupyter kernelspec cmd -h`.

list
    List installed kernel specifications.
install
    Install a kernel specification directory.
uninstall
    Alias for remove
remove
    Remove one or more Jupyter kernelspecs by name.
install-self
    [DEPRECATED] Install the IPython kernel spec directory for this Python.

To see all available configurables, use `--help-all`

其他语言的内核

顺便说一下,与这个问题没有严格联系,但有很多其他可用的内核…https://github.com/jupyter/jupyter/wiki/Jupyter-kernels

sudo apt-get install python3-pip python3-dev
pip3 install -U jupyter

最短最好的解决方案

conda create --name py3-ml python=3.6
conda install ipykernel

source activate py3-ml # or conda activate py3-ml
python -m ipykernel install --user --name py3-ml

要将特定的python添加到jupyter内核中,首先使用以下命令检查可用的python或python3的路径

$ where python3

假设你有'/usr/local/bin/python3'作为路径之一。要为这个版本的python创建一个名为'name_to_new_kernel'的内核,该内核将显示在jupyter中,

$ /usr/local/bin/python3 -m pip install ipykernel
$ /usr/local/bin/python3 -m ipykernel install --user --name name_to_new_kernel

使用实例检查名称为'name_to_new_kernel'的新内核是否添加到jupyter

jupyter kernelspec list

除了Python2之外,我还设法安装了Python3内核。我是这样做的:

在木星上打开一个新的笔记本 复制并运行这里的两个单元格:Enable-Python-3-kernel

最新的工作链接可以在这里找到。

实际代码为:

! mkdir -p ~/.ipython/kernels/python3
%%file ~/.ipython/kernels/python3/kernel.json

{
 "display_name": "IPython (Python 3)", 
 "language": "python", 
 "argv": [
  "python3", 
  "-c", "from IPython.kernel.zmq.kernelapp import main; main()", 
  "-f", "{connection_file}"
 ], 
 "codemirror_mode": {
  "version": 2, 
  "name": "ipython"
 }
}