在Jupyter notebook中切換IPython Kernel

在 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 notebookipython3 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 沒試過,可以試一下。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容