Using jupyter notebooks with a virtual environment



Do you use jupyter notebooks? And virtual environment too, right? And do you know how to use them together? Ah! I got you. It is very simple: follow this guide to learn how to install a custom kernel.


Using virtual environments is important:

  • it helps to maintain your system clean since you don’t install system-wide libraries that you are only going to need in a small project

  • it allows you to use a certain version of a library for one project and another version for another project: if you install the library system-wide and don’t use venv, then you can only use one version of the library

  • it helps reproducible research: you work hard on your project, you write a paper on it because you are an academic research and then you release the code; one year later, the libraries you used are outed but your package still works: by listing the particular versions the you used, another researcher can set up a new virtual environment and use those outdated libraries to reproduce your work

Basically, virtual environments simply your life when you work with Python.

How to do it

I assume that you are a tidy person: you have all your work sorted out in folders; one of these folders contains your very important project and it looks like this:

$ ls

├── data
├── docs
├── src
└── test

Inside this folder create a new virtual environment:

python -m venv projectname

Then activate it:

source projectname/bin/activate

Now, from inside the environment install ipykernel using pip:

pip install ipykernel

And now install a new kernel:

ipython kernel install --user --name=projectname

At this point, you can start jupyter, create a new notebook and select the kernel that lives inside your environment.


$ python -m venv projectname
$ source projectname/bin/activate
(venv) $ pip install ipykernel
(venv) $ ipython kernel install --user --name=projectname

Here is a screencast that shows the whole thing:

Further readings