rgaut
rgaut

Reputation: 3599

How to switch environment on Jupyter notebook for new notebook?

I have an instance with various environment and some notebooks are compatible with different environment for e.g. notebook1 is for MXNet and notebook2 is for Tensorflow.

How do I jump to new environment when I move from notebook1 to notebook2? I tried doing that but this doesn't quite work with Jupyter notebook? Any suggestion?

So I need to do it from conda environment but looks like jupyter notebook UI doesn't respect (calls right activation function) to set the path.

Upvotes: 11

Views: 52502

Answers (5)

Anne
Anne

Reputation: 61

Installing the nb_conda_kernels package as suggested by user @jcb91 worked for me. I did have to install it both in my root (base) environment as well as the virtual environment with which I wanted to use it. To do this, run the following in the Anaconda prompt (in your base environment):

conda install nb_conda_kernels

Then, activate your virtual environment (in the code below named 'myenv') and install the nb_conda_kernels package there as well. You can do this by running in the Anaconda prompt:

conda activate myenv
conda install nb_conda_kernels

You now should be able to switch to your different environment using:

Kernel -> Change Kernel

Upvotes: 4

Travis
Travis

Reputation: 1241

I don't really know the definition and relationship between kernel and conda env, but I beleive we can set a kernel for each env.

I got a problem similar to you: I clean my data in data_cleaning.ipynb under env_without_lgb. However, I have lightgbm installed under env_lightgbm. So, I want to switch kernel/env from env_without_lgb to env_lightgbm, which we can use Kernel -> Change Kernel:

enter image description here

If there is not a kernel you want, create a kernel under the right env according to this guide. For me, I only have a Pure Python 3 kernel and R kernel at first and then I create a kernel under env_lightgbm. At last, we can switch kernel when we editing our code.

However, I still can not figure out the relationship between kernel and env.

Upvotes: 1

rgaut
rgaut

Reputation: 3599

Along with this package I think also need to change the ~/.jupyter/jupyter_notebook_config.py file with following config

c.NotebookApp.kernel_spec_manager_class = 'environment_kernels.EnvironmentKernelSpecManager'

Upvotes: 0

jcb91
jcb91

Reputation: 649

You could use the nb_conda_kernels package, which provides a separate jupyter kernel for each conda environment, along with the appropriate code to handle their setup. This makes switching conda environment as simple as switching jupyter kernel (e.g. from the kernel menu), which I find very convenient. You can get it from the conda-forge channel, using

conda install -c conda-forge nb_conda_kernels

Upvotes: 15

Mark Hannel
Mark Hannel

Reputation: 795

When you start a jupyter notebook within an environment, it will only have access to the modules installed in that particular enviroment. If you need two specific environments for two different notebooks, you will need to start a jupyter notebook within the two environments separately.

If you are using anaconda, you would do the following in a terminal:

source activate MXNET
jupyter notebook

Then in a second terminal:

 source activate Tensorflow
 jupyter notebook

Upvotes: 1

Related Questions