Edit: Just restart Visual Studio Code after creating your virtual environment to make it show up as an option for the Jupyter notebook.

I had my Jupyter notebook open in Visual Studio Code and I just installed matplotlib. When I tried to run import matplotlib.pyplot as plt in one of the cells, I got the ModuleNotFoundError.


Lead Up

Before we dig in to using the virtual environment with a Jupyter notebook, let’s go through the steps of creating a virtual environment and installing all the required modules.

In a new VS Code project, open a terminal and create a new virtual environment with

python -m venv .venv

Activate the new virtual environment


Create a new file with an extension of .ipynb to activate the Jupyter extension.

Install matplotlib with

pip install matplotlib

The Problem

As mentioned at the beginning, importing matplotlib in a cell results in a ModuleNotFoundError.

The Solution

Start by installing ipykernel.

pip install ipykernel

Create a new kernel for Jupyter to use (replace projectname with the name of your project).

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

Restart Visual Studio Code to clear the cache.

Switch to the kernel for the project.

Kernel Switch

You can now import matplotlib and check the version.

Get Version