bluesummers
bluesummers

Reputation: 12627

Different virtualenv's on one Jupyter notebook

I am working on a setup where several developers, working on different projects, all execute their code on a remote machine, using Jupyter notebook.

Since every projects requires a different virtualenv what happens now is that every developer for every projects, sets up a project specific virtualenv, installs notebook to it, runs it on a different port and connects to the remote machine through that port.

Is there a way to have 1 Jupyter notebook running on the remote machine, but be able to choose which virtualenv to use as kernel?

My main consideration is being able to expose only one port on the remote machine, but be able to use different virtual python environment for running the notebooks

Upvotes: 12

Views: 5178

Answers (2)

Harsh
Harsh

Reputation: 166

Is there a way to have 1 Jupyter notebook running on the remote machine, but be able to choose which virtualenv to use as kernel?


This is how I managed to use multiple kernels in the same Jupyter notebook instance

conda install nb_conda

nb_conda is a notebook extention that allows you to manage conda environments from your notebook. It also allows you to switch kernels directly from the Kernal menu.
I have noticed that the above command installs it with a few extras (nbpresent, nb_anacondacloud) which can be optionally disabled.

jupyter-nbextension disable nb_anacondacloud --py --sys-prefix
jupyter-serverextension disable nb_anacondacloud --py --sys-prefix

jupyter-nbextension disable nbpresent --py --sys-prefix
jupyter-serverextension disable nbpresent --py --sys-prefix

If you do not yet use conda you should consider it for your package management and virtualenv needs [source].

I believe that this system does not have many of the pitfalls mentioned in jakesvdp's post that @denfromufa mentions as the notebook extension nb_conda should be dealing with all the internals.


Screenshots

conda environment manager Conda tab in jupyter notebook allows you to manage your environments right from within your notebook.


Change Kernel
You can also select which kernel to run a notebook in by using the Change kernel option in Kernel menu


Upvotes: 0

Matt
Matt

Reputation: 27843

I am working on a setup where several developers

If you have many dev working on a remote machine you must use JupyterHub, JupyterHub is made for that, and JupyterHub is the first step toward easing your pain; if you do not use JupyterHub, things will go wrong.

Once you have JupyterHub installed, your devs will be able to login with their credential wit exposing a single port, and will be able to start/stop notebook servers without sshing in.

Once this is done, you can investigate multiple venv.

In each environment you want to install ipykernel. It is the module that knows how to talk to the notebook. And in each environment you need to issue the python -m ipykernel install --user --name=my-env-name as said in the comments below your posts. This register each env with Jupyter, telling it "Hey I exist expose me to your users". You may also decide to install this that does part of this automatically for you, but have some caveats.

As other commenters have pointed out you likely want to read Jake's post, and if you have several users you should absolutely always almost without questions use JupyterHub.

Upvotes: 11

Related Questions