Integrating Jupyter Notebook in VS Code

Published: Mar 11, 2024 by Kwantae Kim

Open Source Chip Design

Getting Started

  • Integrating Jupyter Notebook in VS Code

Initial version: Jul 4, 2021 (in Korean)
Latest version: Mar 15, 2024

Verified with Windows

Verified with MacOS

Many people use Jupyter Notebook these days as it is easily accessible for developing AI algorithms, like based on PyTorch framework. Jupyter Notebook looks like below. There are some sort of Python codes within each cells.

It you happen to see that one for the first time, it might feel interesting because it can run the code cell-by-cell.



Installing Anaconda

Navigate to Anaconda Webpage and install the Anaconda. Here, you do not need to Python explicitly if you install the Anaconda (Python is included).

There is one thing you need to take extra care. If you proceed the installation of Anaconda like above, it asks you whether you are gonna Add my PATH. I was a bit scared of this red text and it seemed to be kind of warning so I did not mark on this checkbox at the very beginning. But in the end it tortured me for making Anaconda compatible with VS code terminal 😂

So please mark this option as Enabled.



Setting Up Kernel in Anaconda

I thought like below at the first moment.

.. But what is kernel? Why do we set this up?

You will install this package and that package and so on, to run the Python script. These days, newer than 3.6 version of Python is widely used. But if you happend to use older versions of Python, you don’t want to uninstall Python environment and reinstall. Kernel works greatly in this case. You can build a virtual environment with old Python version and install relevant packages within that kernel only.

Start Anaconda Navigator and add a kernel in the Environments tab using Create button.



Installing Jupyter in a New Kernel

If you click your new kernel, you will see something is rotating and the button like play is positioned differently. Then as you click the play button, you will see several tabs as below. It will only show Open Terminal and Open with Python at the first time. Let’s make it show all the options.

Use Open Terminal and open Terminal. Execute the below commands and install Jupyter and ipykernel.

$ pip install jupyter
$ pip install ipykernel

ipykernel (Jupyter was called ipy before) is needed to work on our kernel, later when we play with Jupyter Notebook. Even though we set up a new kernel as above, it was set up in Anaconda, not in Jupyter.

As a next step, execute the below command and let Jupyter know that we are gonna use your new kernel. Replace {Your Kernel} with your kernel name (without { and }). Here, -m means running package.

$ python -m ipykernel install --user --name {Your Kernel}

But even at this point, in my case, integrating Anaconda with VS code did not work correctly. So I further executed the following commands to make sure they are seamlessly integrated each other.

$ conda install jupyter
$ conda install ipykernel --update-deps
$ conda search python
$ conda install python=3.8.10

Here, if you run search python then it shows a list of all the possible Python versions. I chose 3.8.10 on purpose, even though 3.9 or newer version is already released. The most important thing here is, we need to select the python version that is currently compatible with VS code. Otherwise, it keeps causing error as below and it drives us crazy 😡

kernel died with exit code 1

I selected Python version as 3.9 at the very beginning of this setup but it caused the above error all the time. I tried several methods here and there on the webpages but I couldn’t make it work. So the option I chose was resetting Windows as factory-reset 😳 and proceed every steps from the beginning. Before resetting my Windows, I saw the working-fine-version of Python was 3.8.10 so I chose 3.8.10 in my kernel for running VS code.

📣 As of 15-Mar-2024, version 3.11.5 is working fine 👍🏼



Configuring VS Code

I am familiar with using VS Code for editing. So I thought it would be wonderful to set up the environment Jupyter Notebook within the VS code.

  1. Go to Extensions and install Jupyter (by Microsoft) and Python in VS Code.
  2. Open palette by Ctrl + Shift + P (in Mac, ⌘ + Shift + P)
  3. Select Python: Select Interpreter
  4. You will find all available kernels. Select your kernel. Then you can work with the Python as we set up in our Anaconda kernel.
  5. (Optional for Windows) Open terminal by Ctrl + ` and set the terminal as cmd, not PowerShell
    For making cmd as default, open palette by Ctrl + Shift + P and use Terminal: Select Default Profile
  6. Execute following command and check whether the terminal in the VS code shows your kernel name.
$ conda activate {Your Kernel}



Enjoy Jupyter Notebook in VS Code

All set! Open palette and try Jupyter: Create New Blank Notebook.

Latest Posts