How to use Python Virtual Environments in Jupyter Notebook

What are Python Virtual Environments in Jupyterand what are they used for? Well, instead of reinventing the wheel, let’s see what the Python Docs have to say about it:

Python applications will often use packages and modules that don’t come as part of the standard library. Applications will sometimes need a specific version of a library, because the application may require that a particular bug has been fixed or the application may be written using an obsolete version of the library’s interface.

This means it may not be possible for one Python installation to meet the requirements of every application. If application A needs version 1.0 of a particular module but application B needs version 2.0, then the requirements are in conflict and installing either version 1.0 or 2.0 will leave one application unable to run.

The solution for this problem is to create a virtual environment, a self-contained directory tree that contains a Python installation for a particular version of Python, plus a number of additional packages.

Different applications can then use different virtual environments. To resolve the earlier example of conflicting requirements, application A can have its own virtual environment with version 1.0 installed while application B has another virtual environment with version 2.0. If application B requires a library be upgraded to version 3.0, this will not affect application A’s environment.

PytHon docs | Virtual Environments and Packages

Creating your Virtual Environment

If you are using Python 2, you can install Virtualenv using your command line application with (change venv to whatever name you’d like):

pip install --user virtualenv
virtualenv venv

Python 3.3 has native support for this operation :

python -m venv venv

After you’ve created your environment, activate it with:

source venv/bin/activate

To deactivate your virtual environment, run deactivate. To delete, simply remove the folder with the virtual environment, rm -r venv.

For further information, take a look at the Virtualenv or Venv Documentation.

Using Anaconda?

Anaconda is a Python and R tool designed to simplify package management and, thereby, speed up your development. Start by installing the Anaconda package. Now you can create your environment with:

conda create -n venv

You can specify a different version like so:

conda create -n venv python=3.1

Now you can activate your new environment:

conda activate venv

Similarly to our previous example, deactivate the environment with conda deactivate and remove with conda env remove -n venv.

More detailed information available in the Anaconda Documentation.

Add Packages to your Virtual Environment

Now that you have your environment installed, you’ll notice it is a fresh install of Python. Make sure you have activated your environment as shown above (source venv/bin/activate / conda activate venv), and then add any packages you are going to work with using your favorite package installer (e.g. pip install package)

Connecting to Jupyter

First, activate your environment with source venv/bin/activate or conda activate venv. Next, install iPyKernel which provides the IPython kernel for Jupyter:

pip install --user ipykernel

Add your kernel to Jupyter with:

python -m ipykernel install --user --name=venv

You should get the following feedback:

Installed kernelspec myenv in /home/user/.local/share/jupyter/kernels/venv

Now you can select your kernel from your Jupyter notebook!

Virtual Environments in Jupyter
Your Virtual Environment is now available

If you’d like to disconnect your kernel in the future, use jupyter kernelspec uninstall venv

Happy coding!

If you’re having trouble getting your Jupyter Notebook up and running, take a look at this article on How to fire up a Jupyter Notebook on Mac.