How to join conda env into jupyter notebook

Posted by Kuihao on 2022-04-18
Estimated Reading Time 2 Minutes
Words 460 In Total
Viewed Times

How to join conda env into jupyter notebook

來源:

  1. ref. medium
  2. ipython doc
  3. https://jupyter.org/install

安裝 Jupyter (Jupyter Lab, Jupyter Notebook)

首先要有安裝 jupyter notebook/lab,兩者挑一個安裝即可,
我建議用 conda 建一個只用來建制 Jyputer Server 的環境, 以方便管理
(anaconda 已內建、miniconda 要自己裝)

Jupyter Notebook (類似 google colab)

install the classic Jupyter Notebook

1
pip install notebook

To run the notebook:

1
jupyter notebook

JupyterLab (有整合 command line)

Install JupyterLab with pip:

1
pip install jupyterlab

To run JupyterLab:

1
jupyter-lab

可透過已下命令,可於任意虛擬環境查看目前 jupyter 已連動的 kernel

1
jupyter kernelspec list

將指定的 comda python 環境加進 jupyter kernel

  1. Create a Conda environment.
1
conda create --name <myenv>

or create a tensorflow with gpu env:

1
conda create -n <myenv-gpu> python=<version> tensorflow-gpu
  1. Activate <myenv>
1
conda activate <myenv>

install any package you need in this environment.

1
pip install <package>
  1. Set this conda environment on your jupyter notebook, to do so please install ipykernel. (install in this myenv)
1
2
3
conda activate myenv # `source activate myenv` in linux, 若已開啟 myenv 則不用這行
conda install pip
conda install ipykernel # or pip install ipykernel

可透過已下命令,可查看目前虛擬環境允許被 jupyter 連動 (當啟動 jyputer notebook 會自動帶入此環境為 jyputer kernel)

1
jupyter kernelspec list
  1. Install a Python environment Kernel in a myenv.
1
2
conda activate myenv # `source activate myenv` in linux, 若已開啟 myenv 則不用這行 
python -m ipykernel install --user --name <this-myenv-name> --display-name "name-you-want-show-in-notebook"

當然,你也可以為其他虛擬環境增加 ipykernel (使 jupyter notebook 可以使用此 env 作為 kernel)

1
2
conda activate other-env # `source activate other-env` in linux, 若已開啟 myenv 則不用這行 
python -m ipykernel install --user --name <this-env-name> --display-name "Python (other-env)"

移除 ipykernel

  • 列出目前 jupyter 追蹤的虛擬環境與其路徑 (光是刪除 conda env 是無效的,還必須將其從清單中移除)
1
jupyter kernelspec list

移除對應的 env-name

1
jupyter kernelspec remove <myenv>