Try   HackMD

How to join conda env into jupyter notebook (Installing Jupyter&Kernel)

Related: Jupyter 新增虛擬環境為新 kernel
來源:

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

安裝 Jupyter (Jupyter Lab, Jupyter Notebook)

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

Jupyter Notebook (類似 google colab)

install the classic Jupyter Notebook

pip install notebook

To run the notebook:

jupyter notebook

JupyterLab (有整合 command line)

Install JupyterLab with pip:

pip install jupyterlab

To run JupyterLab:

jupyter-lab

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

jupyter kernelspec list

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

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

or create a tensorflow with gpu env:

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

install any package you need in this environment.

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

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

jupyter kernelspec list
  1. Install a Python environment Kernel in a myenv.
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)

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 kernelspec remove <myenv>