193

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.

Seanny123
  • 8,776
  • 13
  • 68
  • 124
Statistic Dean
  • 4,861
  • 7
  • 22
  • 46

7 Answers7

356

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.

Nihal Sangeeth
  • 5,168
  • 2
  • 17
  • 32
  • 10
    I direct people having difficulties getting a tensorflow environment to work in jupyter lab/notebook to this answer. It worked for me. Thanks. – aez Aug 18 '19 at 13:07
  • 2
    I would only add that once you have the new kernel, go to your jupyter notebook and, under "kernel", select "change kernel" to your newly created kernel. Once there you can use things like import tensorflow as tf if your environment was a tensorflow environment. I also recommend this for people getting to a tensorflow environment form jupyter. I redirected multiple questions on that to here. – Ivan Oct 09 '19 at 01:21
  • 8
    why doesn't jupyter lab just inherit the environment as jupyter notebook does for me? Anyway this fixed my problem so thanks for that. – Freddy Nov 20 '19 at 08:28
  • 1
    It helps to name the kernel so that it specifies which environment/use it is tied to. – ohailolcat Sep 22 '20 at 14:33
  • 3
    While is an effective answer, I'd still recommend the `nb_conda_kernels` approach instead, since it avoids the manual registration step, which can be prone to mistakes. – merv May 21 '22 at 04:04
  • In my case `ipython kernel install --user --name=` didn't do the trick; I had to do `ipython kernel install --user --name 'some-name' --display-name "Name that I will see in the Laucher"` – Valentin_Ștefan Jun 30 '22 at 17:45
  • 1
    @merv FYI: `nb_conda_kernels` sucks, see: https://github.com/Anaconda-Platform/nb_conda_kernels/issues/216. (an issue that has been pending for a year without anyone responsible for it.) – NeoZoom.lua Apr 03 '23 at 08:19
  • It works for me, Jupyterlab 4.0.3; How can I remove the kernels? – wm3 Jul 21 '23 at 02:02
55

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.

Statistic Dean
  • 4,861
  • 7
  • 22
  • 46
  • 2
    Sadly this doesn't seem to work (jupyter lab version 1.1.4 with python 3.7.4) - use the accepted answer above to install the kernel. – philmaweb Oct 09 '19 at 13:41
  • 1
    Works for me with Jupyter Lab 1.1.4, Python 3.7.3, and [nb_conda_kernels](https://github.com/Anaconda-Platform/nb_conda_kernels) 2.2.2. No need to "install" the kernel, except in the environment that you want to access in your notebook. – sappjw Oct 10 '19 at 16:13
  • 2
    @sappjw The accepted answer works but this one lacks `$ ipython kernel install --user --name=` and did not see the new kernel until I did this – Pherdindy Mar 02 '20 at 11:59
  • @Pherdindy the difference is that this answer relies on nb-conda_kernels to detect the conda environment rendering the `ipython kernel install line` unnecessary – Statistic Dean Mar 04 '20 at 14:14
  • 1
    I prefer this method as you can be running a notebook, install a new package and have it immediately reflected in the notebook – Aus_10 Jun 18 '20 at 19:17
  • 1
    This works great for me on Mac and PC, from 2019 to 2022 – Travis Apr 30 '22 at 11:19
  • 1
    @merv When I found your comment anywhere I immediately know that I'm probably on the right track, thanks! But it seems that the `nb_conda_kernels` project has been abandoned on GitHub? – NeoZoom.lua Apr 03 '23 at 04:47
25

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!

  • 3
    This worked for me when all else failed. Thanks! Still not clear on why nb_conda_kernels doesn't seem to automatically do the job for me anymore. Note that, in my experience, if you have ipykernel, jupyterlab, and nb_conda_kernels installed in your base environment and launch JupyterLab from within the base environment, it is more likely to see all available conda kernels, weirdly. – emigre459 Jul 10 '20 at 20:40
  • Did this too for a new conda env that wasn't showing up, further adjusted the generated `kernel.json` by referring to other existing conda envs in `~/.local/share/jupyter/kernels/` – tnwei Jun 13 '21 at 02:27
  • I agree this is a slightly less error-prone version of https://stackoverflow.com/a/53546634/570918, but more robust still is the `nb_conda_kernels` approach, which will automatically detect any environments with `ipykernel` (or other language-specific kernel packages) installed. – merv May 21 '22 at 04:07
6

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
Iva
  • 2,447
  • 1
  • 18
  • 28
Evo Mars
  • 61
  • 1
  • 2
1

For the people who are looking for answer in 2023, the following works:

Assuming the name of the virtual environment is new_venv1. Run the following command in terminal such as bash.

Follow the below steps:

  1. Activate your newly created virtual environment.\n $:> conda activate new_venv1
  2. $:> conda install ipykernel
  3. $:> python -m ipykernel install --user --name new_venv1 --display-name="New virtual environment"
  4. Finally restart your jupyter.

Note: Do pay attention to --name flag and --display-name flag.

Credits: medium article

Ashish Johnson
  • 379
  • 4
  • 16
0

The following worked for me

pip install nb_conda

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

Ryan M
  • 18,333
  • 31
  • 67
  • 74
jfran
  • 143
  • 1
  • 7
-4

useful cheat sheet from conda. CONDA CHEAT SHEET

  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/33853465) – Ran A Feb 21 '23 at 14:03