How to add conda environment to jupyter lab

PythonAnacondaJupyter NotebookJupyter Lab

Python Problem Overview


I'm using Jupyter Lab and I'm having trouble to add conda environment. The idea is to launch Jupyter Lab from my base environment, and then to be able to choose my other conda envs as kernels.

I installed the package nb_conda_kernels which is supposed to do just that, but it's not working as I want. Indeed, let's assume I create a new Conda Environment, then I launch jupyter lab from base, I can't see the new environment as an available kernel.

I have found a "fix", which works everytime but is not convenient at all. If I install Jupyter Notebook in my new environment, then launch a jupyter notebook from this new environment, close it, go back to base environment, and then launch Jupyter Lab from base environment, my new environment is available as a kernel in Jupyter Lab.

If you know how to make it work without this "fix", I would be very grateful.

Python Solutions


Solution 1 - Python

Assuming your conda-env is named cenv, it is as simple as :

$ conda activate cenv           # . ./cenv/bin/activate in case of virtualenv
(cenv)$ conda install ipykernel
(cenv)$ ipython kernel install --user --name=<any_name_for_kernel>
(cenv)$ conda deactivate

If you restart your jupyter notebook/lab you will be able to see the new kernel available. For newer versions of jupyter kernel will appear without restarting the instance. Just refresh by pressing F5.

PS: If you are using virtualenv etc. the above steps hold good.

Solution 2 - Python

A solution using nb_conda_kernels. First, install it in your base environment :

(base)$ conda install -c conda-forge nb_conda_kernels

Then in order to get a kernel for the conda_env cenv :

$ conda activate cenv
(cenv)$ conda install ipykernel
(cenv)$ conda deactivate

You will get a new kernel named Python [conda env:cenv] in your next run of jupyter lab / jupyter notebook

Note : If you have installed nb_conda_kernels, and want to create a new conda environment and have it accessible right away then

conda create -n new_env_name ipykernel

will do the job.

Solution 3 - Python

I tried both of the above solutions and they didn't quite work for me. Then I encountered this medium article which solved it: https://medium.com/@jeremy.from.earth/multiple-python-kernels-for-jupyter-lab-with-conda-c67e50de3aa3

Essentially, after running conda install ipykernel inside of your cenv environment, it is also good to run python -m ipykernel install --user --name cenv within the cenv environment - that way, we make sure that the version of python that is used within the jupyter environment is the one in cenv. Cheers!

Solution 4 - Python

The following worked for me

pip install nb_conda

https://github.com/Anaconda-Platform/nb_conda

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionStatistic DeanView Question on Stackoverflow
Solution 1 - PythonNihal SangeethView Answer on Stackoverflow
Solution 2 - PythonStatistic DeanView Answer on Stackoverflow
Solution 3 - PythonDaniel Firebanks-QuevedoView Answer on Stackoverflow
Solution 4 - PythonjfranView Answer on Stackoverflow