Using different conda environments with Jupyter Lab

Run Jupyter lab from base and toggle between conda envs

python
jupyter
conda
Author

Mark Isken

Published

April 3, 2022

This has been a source of confusion for me over the years. Previously, I had been installing jupyter and jupyter-lab into each of my conda virtual envs. However, there is really no need to do this and after digging through many blog and StackOverflow posts (links at bottom), I’ve settled on the following procedure. The tl;dr version is:

Create conda virtual env for analytics

Here’s my YAML file. IMPORTANT - notice that we are NOT installing jupyter and/or jupyter-lab in our new conda virtual env. Instead, we are installing ipykernel which will then allow Jupyter (run from base env) to automatically “see” this kernel associated with the analytics virtual env and allow us to switch our notebook to this kernel.

name: analytics
channels:
  - defaults
  - conda-forge
dependencies:
  - numpy
  - pandas
  - scipy
  - scikit-learn
  - matplotlib
  - seaborn
  - ipykernel
  - simpy
  - networkx
  - sphinx
  - cookiecutter
  - pyyaml
  - pytest
  - pip

Create the virtual env with:

conda env create -f analytics.yml

Install nb_conda_kernels in base environment

In order for Jupyter to see any kernels associated with conda environments, we need to install nb_conda_kernels in our base environment. As described here in the conda docs, by installing nb_conda_kernels a few other Jupyter extensions also get installed, including nb_conda. The nb_conda extension is NOT the same as nb_conda_kernels and appears to provide a conda navigator like tab in Jupyter.

conda install nb_conda_kernels

Run Jupyter Lab from base environment

Always run Jupyter Lab from base environment. We don’t even have Jupyter Lab installed in the analytics virtual environment. Once Jupyter is running, the kernel can be set to any one of the kernels associated with your conda virtual envs. You can do this from the Kernel menu or from the upper right corner of the window.

Resources

I settled on this procedure after reading numerous blog posts and SO posts. Some of the most helpful are:

Reuse

Citation

BibTeX citation:
@online{isken2022,
  author = {Mark Isken},
  title = {Using Different Conda Environments with {Jupyter} {Lab}},
  date = {2022-04-03},
  langid = {en}
}
For attribution, please cite this work as:
Mark Isken. 2022. “Using Different Conda Environments with Jupyter Lab.” April 3, 2022.