Loading python venvs (adding environment_kernels to JupyterHub)

Hello all,

I’d like to allow jupyterhub users to load kernels from their own python virtualenvs. Mainly I’d like to let them install their packages in a virtualenv and the load it in the hub.

I followed some instructions that, basically, say that, after activating the python virtual env, one has to install the ipykernel and then add the venv to jupyter:

(venv) $ python -m pip install --user ipykernel
(venv) $ python -m ipykernel install --user --name=myenv

(someone uses the ipython instead of ipykernel to make the kernel available in jupyter, not sure what teh difference is between using ipykernel and ipython)

With the above commands I can later see my new kernel in the hub. So far so good.

The problem comes when one inspect the environment in the kernel: the PATH variable does not include the bin directory from the python venv. This is something someonbe already commented here: https://github.com/jupyterhub/jupyterhub/issues/430#issuecomment-184978527

Again, I’m not sure if the reason is that some variables are passed to the spawner in the conf file:

c.Spawner.env_keep = [
'PATH',
'PYTHONPATH',
'LD_LIBRARY_PATH',
'LIBRARY_PATH',
'VIRTUAL_ENV',
'LANG',
'LC_ALL',
'HTTP_PROXY',
'HTTPS_PROXY',
'NO_PROXY',
]

In my case I need to keep PATH cause python itself is not installed in a default location.

According to the above link, the solution seems to be https://github.com/Cadair/jupyter_environment_kernels : “A Jupyter plugin to enable the automatic detection of environments as kernels.”.

So, after insatlling the package environment_kernels with pip I’ve added at the end of my jupyterhub_config.py the line:

c.NotebookApp.kernel_spec_manager_class = 'environment_kernels.EnvironmentKernelSpecManager'

and then restarted jupyterhub.
I’ve also installed my python venv in ~/.virtualenvs (as this is supposed to be the default directory where virtual_envs packages searches for custom virtual_envs), but the venvs does not show up in jupyterhub.

I know this question could be asked to the jupyter_environment developer but I’m wonering if any of you alredy solved the virtual_env PATH issue in jupyterhub by using jupyter_environments, if yes how you did enable it, or using any other method to load the python virtual env.

TIA,
Arnau