Posts Using virtualenv with Jupyter Notebooks
Post
Cancel

Using virtualenv with Jupyter Notebooks

When using a Jupyter I also want to use virtualenv to keep my different python environments isolated. But I always forget how I do this, so here is how.

Create a new virtual environment or enter an already existing one and install the ipykernel. The following code creates a new one, to enter an existing one use workon my_env.

1
2
3
$ mkvirtualenv my_venv
$ pip install ipykernel
$ python -m ipykernel install --user --name my_venv

Then you can start your juypter lab or notebook in another terminal.

1
2
$ jupyter lab
$ jupyter notebook

Follow the generated link and choose the kernel called my_env.

If you need to install a python package, e.g. matplotlib, just install it into your virtual environment to be able to use it in jupyter.

1
$ pip install matplotlib

Additional

If you are using ArchLinux, btw. it just got 18 years old (initial release was 11 March 2002), you can install Jupyter Notebook or Jupyter Lab via pacman.

1
2
$ sudo pacman -S jupyterlab
$ sudo pacman -S jupyter-notebook
This post is licensed under CC BY 4.0 by the author.