提要
我們可以通過conda創(chuàng)建多個(gè)python環(huán)境,在使用時(shí)通過activate 命令切換環(huán)境.在使用jupyterlab的時(shí)候我們需要在不同環(huán)境間切換來運(yùn)行代碼,因此需要?jiǎng)?chuàng)建多個(gè)jupyter的kernel來完成jupyter的conda環(huán)境切換
安裝kernel
比如現(xiàn)在我新建了個(gè)conda環(huán)境
conda create -n th python=3.7
該環(huán)境th是用來跑pytorch代碼的
切換環(huán)境到th
activate th
在th環(huán)境下安裝ipykernel
(想讓jupyter使用該環(huán)境就必須在該環(huán)境下安裝ipykernel)
pip install ipykernel
通過ipykernel就可以安裝kernel
python -m ipykernel install --name th
其中th為kernel的名字,可以任意取,不過建議和conda環(huán)境名一樣方便確認(rèn).
在哪個(gè)conda環(huán)境下執(zhí)行以上命令,就會生成該環(huán)境的kernel
安裝jupyterlab
安裝jupyterlab時(shí)可以切換回默認(rèn)的環(huán)境,這樣每次啟動不用切換環(huán)境
activate base
pip install jupyterlab
運(yùn)行 jupyterlab
jupyter-lab

如上th的kernel成功安裝后就會顯示在右側(cè),大功告成
補(bǔ)充
查看kernel命令
jupyter-kernelspec list
刪除kernel命令
(conda環(huán)境刪除后,相應(yīng)的kernel卻不會刪除,使用kernel會報(bào)錯(cuò),所以刪除conda環(huán)境后,應(yīng)一并刪除相應(yīng)kernel)
jupyter kernelspec remove th
th為kernel名