Add a conda env to a running jupyter server

Jun 1, 2021

As part of my research, I work on several remote machines, which usually are accessible through jupyter-hub.

Sometimes, I need a particular version of a package that cannot simply be pip or conda installed due to some limitations (e.g. binaries) or version compatibility issues with other packages. In these cases, it necessary to add a new kernel.

Unfortunately, it is impossible to create a new conda environment and see it as an available kernel when using a running instance of jupyter on jupyter-hub.

Specifically, since the server has already started and new envs are only checked on startup unless the container is respawned (not paused and restarted), the new kernel will not be visible in the dropdown menu in the notebook.

To access the new env, do the following.

  1. Create the environment according to your specification:
conda create --name ENV-NAME python=3.7 pandas==0.25

then in your notebook set this environment to have a higher priority than the base env by prepending it to the path.

import os, sys

sys.path.insert(0, '/opt/conda/envs/ENV-NAME/lib/python3.7/site-packages')

import pandas as pd
pd.__file__  # should now point to pandas stored in `ENV-NAME`