Creating notebook kernels with virtualenv

13 March 2018  

Python, jupyter  

Instructions  

What to do when you want to combine jupyter notebooks and virtual environments?

Install virtual environment wrapper

The wrapper is installed using pip:

pip install virtualenvwrapper

The configuration is done by setting up the environment directory and sourcing virtualenvwrapper.sh:

export WORKON_HOME=~/Envs
mkdir -p $WORKON_HOME
source /usr/local/bin/virtualenvwrapper.sh

The previous export and source commands can be added in the .bashrc file so that they are executed automatically.

Create the new virtual environment

Note that the python version used in the virtualenv is specified using the --python option:

mkvirtualenv --python=/usr/local/bin/python3.6 3Dplot

Create the new kernel

The instructions are found in the ipython documentation.
Both the environment and the display names (visible in the notebook interface) can be specified:

pip install ipykernel
python -m ipykernel install --user --name 3Dplot --display-name "3D plotting"

Open jupyter-notebook and select the kernel

jupyter-notebook

When you create a new notebook, a drop-down allows you to select the kernel or virtualenv of your choice.
From an existing notebook window, you can also modify the kernel by clicking on Kernel / Change kernel.