前言

Jupyter Notebook 作为一个把代码、图像、注释、公式和作图集于一处,从而实现可读性分析的交互式笔记本工具,借助所谓的内核(Kernel)的概念,Jupyter Notebook 可以同时支持包括 python2、python3、R、Ruby 在内超过50多种不同编程环境。基于Kernel,Jupyter Notebook 可以支持的编程语言可以参考列表

目的

在 Jupyter Notebook 中实现 python2 与 python3 自由切换。

步骤

参考:https://stackoverflow.com/questions/30492623/using-both-python-2-x-and-python-3-x-in-ipython-notebook

The idea here is to install multiple ipython kernels. Here are instructions for anaconda. If you are not using anaconda, I recently added instructions using pure virtualenvs.

Anaconda 4.1.0

Since version 4.1.0, anaconda includes a special package nb_conda_kernels that detects conda environments with notebook kernels and automatically registers them. This makes using a new python version as easy as creating new conda environments:

  1. conda create -n py27 python=2.7 ipykernel
  2. conda create -n py36 python=3.6 ipykernel

After a restart of jupyter notebook, the new kernels are available over the graphical interface. Please note that new packages have to be explicitly installed into the new enviroments. The Managing environments section in conda’s docs provides further information.

Manually registering kernels

Users who do not want to use nb_conda_kernels or still use older versions of anaconda can use the following steps to manually register ipython kernels.

configure the python2.7 environment:

  1. conda create -n py27 python=2.7
  2. source activate py27
  3. conda install notebook ipykernel
  4. ipython kernel install --user

Additionally you can pass the --name and --display-name options to ipython kernel install if you want to change the names of your kernels. See ipython kernel install --help for more informations.

总结起来就是:

  1. 在 python2 和 python3 环境中分别安装 ipykernel。
  2. 手动为当前用户(—user),或者指定路径(—prefix)安装 IPython kernel spec。

需要注意的是:

  • 其中 --user--prefix 不能同时使用。
  • --user ,IPython kernel spec 默认安装到当前用户 ~/.local/share/jupyter/kernels 或者 ~/.ipython/kernels
  • 不加 --user--prefix ,IPython kernel spec 默认安装到 /usr/local/share/jupyter/kernels 或者 /usr/share/jupyter/kernels
  • IPython kernel spec 安装可以使用 python -m ipykernel install ,也可以使用 ipython-kernel-install 命令,两者是一样的。
    Jupyter Notebook 教程(三):多语言编程环境配置 - 图1

实战

使用 --prefix 指定 IPython kernel spec 安装路径:

  1. # python2 环境(默认)
  2. $ python -m pip install ipykernel
  3. $ python -m ipykernel install --prefix=/Bio/Anaconda2.5/
  4. # python3 环境
  5. $ /Bio/Anaconda2.5/envs/python3/bin/python -m pip install ipykernel
  6. $ /Bio/Anaconda2.5/envs/python3/bin/python -m ipykernel install --prefix=/Bio/Anaconda2.5/ --name "python3"

也可以使用 conda 进行安装:

  1. # python2 环境(root)
  2. conda install -n root ipykernel
  3. python -m ipykernel install --prefix=/path/to/install --name "python2"
  4. # python3 环境
  5. conda install -n python3 ipykernel
  6. /path/to/python3 -m ipykernel install --prefix=/path/to/install --name "python3"

最后,jupyter-kernelspec list 检查安装好的 kernels,启动 jupyter notebook

  1. $ jupyter-kernelspec list
  2. Available kernels:
  3. python2 /Bio/Anaconda2.5/share/jupyter/kernels/python2
  4. python3 /Bio/Anaconda2.5/share/jupyter/kernels/python3

Jupyter Notebook 教程(三):多语言编程环境配置 - 图2

  1. 在 python2 和 python3 环境中分别安装 ipykernel。
  2. 手动为当前用户( --user ),或者指定路径( --prefix )安装 IPython kernel spec。其中 --user--prefix 不能同时使用。
  3. 使用 --user 时,IPython kernel spec 默认安装到当前用户 ~/.local/share/jupyter/kernels 或者 ~/.ipython/kernels
  4. 不加 --user--prefix ,IPython kernel spec 默认安装到 /usr/local/share/jupyter/kernels 或者 /usr/share/jupyter/kernels

IPython kernel spec 安装可以使用 python -m ipykernel install ,也可以使用 ipython-kernel-install 命令,两者是一样的。