The Python Oracle

How to add conda environment to jupyter lab

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: RPG Blues Looping

--

Chapters
00:00 How To Add Conda Environment To Jupyter Lab
00:55 Answer 1 Score 380
01:25 Accepted Answer Score 62
01:59 Answer 3 Score 27
02:28 Answer 4 Score 9
02:48 Thank you

--

Full question
https://stackoverflow.com/questions/5300...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #anaconda #jupyternotebook #jupyterlab

#avk47



ANSWER 1

Score 380


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.




ACCEPTED ANSWER

Score 62


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.




ANSWER 3

Score 27


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!




ANSWER 4

Score 9


I couldn't get conda environment to show up in jupyter lab as well and for me worked only this: (assuming as above 'cenv' as environment name)

  1. conda activate cenv
  2. conda install ipykernel
  3. python -m ipykernel install --user --name=cenv