How to setup a Jupyter Notebook in VS Code (w/ virtual env & kernels) & install packages.

Claudia Nikel
4 min readJan 22, 2024

--

Steps

1. Install Jupyter

You will need to install Jupyter in order to open a Jupyter notebook. You can install using Pip, Anaconda or Conda depending on your requirements (see here).

pip3 install jupyter

2. Install Jupyter extension in VS Code

When inside VS Code, click on the Extensions button on the left side bar. Then search for the extension called Jupyter and click install

3. Create a project folder & navigate into it

First you will need to create a project folder or navigate into a folder you have already created. You can use Finder, Terminal, or do it in VS Code. Below is the code to create a folder called myproject from within the terminal:

mkdir myproject

Navigate into the project folder you just created with VS Code by clicking on the button bellow.

4. Create & activate a virtual environment

Open up a terminal within VS Code and make sure you are within the correct project folder.

Now you need to create a virtual environment inside of the project folder. The code below will create a virtual environment called my_virtual_env when run inside the terminal:

python3 -m venv my_virtual_env

You should be able to double check that you have created the virtual environment properly if you go into your project folder on your computer and see a new folder named after your virtual environment.

When you navigate inside of the virtual environment you should see the following folders:

You can also double check this within the terminal using:

# navigate into the virtual environment
cd my_virtual_env

# list out files in directory
ls

It should look like this:

Once you have created the virtual environment you need to activate it. The code below will activate your newly created virtual environment:

source my_virtual_env/bin/activate

You should now see your virtual environment activated within your terminal like below:

5. Install ipykernal

Now you need to install ipykernal within the virtual environment.

pip3 install ipykernel

6. Create new kernel

Now you need to create a new kernel to be used by your project. The below code will create a kernel called myproject_kernel :

python3 -m ipykernel install --user --name=myproject_kernel

7. Start & open Jupyter notebook

Now you need to open up a Jupyter notebook. You can do that directly in VS Code using the code below:

jupyter notebook

If it was successful then you should see this message in your terminal:

Now you need to open a Jupyter notebook. Open the VS Code search bar by typing cmd+shift+p . Type in & choose: “Create: New Jupyter Notebook”.

8. Select correct kernel for project

Now you need to select myproject_kernel kernel that you created. Open the VS Code search bar again and type cmd+shift+p. Type in & choose: “Notebook: Select Notebook Kernel” :

Select “Jupyter Kernal..” :

Then select myproject_kernel :

Now you should see your kernel running in the top right of your notebook:

9. Install package within your Jupyter notebook

Use the code below to install your package (in this case pandas) onto your virtual environment:

!pip install pandas

10. Import package

Import the package in your notebook using the code below:

import pandas

11. Use your package

Now your notebook is set up and running in your virtual environment. Your package is ready to be used.

Happy Coding

--

--