在Jupyter notebook中切换IPython Kernel

在 Jupyter notebook 新建 Notebook 菜单下有 python2、python3可选,且在Kernel --> change Kernel 中可以切换执行的内核.

首先,需要分别在 python2,python3的环境下安装ipykernel:

conda install ipykernel

如果用的是anaconda3版本,那么 Python3环境下的 ipykernel 已经装好了。然后分别建 kernel :

ipython kernel install
ipython3 kernel install

这样在/anaconda3/share/jupyter/kernels/ 目录下有 python2、python3两个文件夹,可以查看kernel.json的内容

{
 "argv": [
  "/anaconda3/bin/python",
  "-m",
  "ipykernel_launcher",
  "-f",
  "{connection_file}"
 ],
 "display_name": "Python 3",
 "language": "python"
}

现在运行jupyter notebook,ipython notebookipython3 notebook就能切换了。第一次切换可能会报错:

错误信息

conda install autopep8之后再打开 Jupyter notebook。尝试切换到python2下:

import numpy
print numpy.__version__

如果你有自己独特的 python 可执行环境,可以尝试:

path/to/python -m ipykernel install <options>

conda create -n py36-test python=3.6
source activate py36-test
python -m ipykernel install --name py36-test
source deactivate

这样下拉菜单里就有py36-test这个 kernel 了.

参考Using both Python 2.x and Python 3.x in IPython Notebook获得更多解释。

一个不太相关的 tips:把文件夹拖进终端就能得到绝对路径,如果是 zsh 直接回车就进入指定目录了!方不方便,快不快捷!其他 shell 没试过,可以试一下。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。