在 Jupyter notebook 新建 Notebook 菜單下有 python2、python3可選,且在Kernel --> change Kernel 中可以切換執(zhí)行的內(nèi)核.
首先,需要分別在 python2,python3的環(huán)境下安裝ipykernel:
conda install ipykernel
如果用的是anaconda3版本,那么 Python3環(huán)境下的 ipykernel 已經(jīng)裝好了。然后分別建 kernel :
ipython kernel install
ipython3 kernel install
這樣在/anaconda3/share/jupyter/kernels/ 目錄下有 python2、python3兩個文件夾,可以查看kernel.json的內(nèi)容
{
"argv": [
"/anaconda3/bin/python",
"-m",
"ipykernel_launcher",
"-f",
"{connection_file}"
],
"display_name": "Python 3",
"language": "python"
}
現(xiàn)在運行jupyter notebook,ipython notebook或ipython3 notebook就能切換了。第一次切換可能會報錯:

錯誤信息
conda install autopep8之后再打開 Jupyter notebook。嘗試切換到python2下:
import numpy
print numpy.__version__
如果你有自己獨特的 python 可執(zhí)行環(huán)境,可以嘗試:
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獲得更多解釋。
一個不太相關(guān)的 tips:把文件夾拖進終端就能得到絕對路徑,如果是 zsh 直接回車就進入指定目錄了!方不方便,快不快捷!其他 shell 沒試過,可以試一下。