Jupyter Notebooks in VS Code

shamal desilva
3 min readMar 24, 2022

--

The fact that your reading this tells me that your are interested in data analytics and your preferred tool for these tasks are Jupyter Notebooks. Jupyter Notebooks are a great way to get started with data analytics as it provides a way to run sections of your code and view the individual output of each of them. However conventional Jupyter Notebooks have a considerable amount of shortcomings that make it a somewhat cumbersome tool to use in a professional setting.

Using Jupyter Notebooks in VS Code greatly reducers these shortcomings as your able to make use of VS Code features such as Split view , Code Debugging , Linting and Style correction. In this short article will go through the exact steps you need to take to set up a Jupyter Notebook inside a python virtual environment within VS Code in 3 easy steps.

STEP 01

Set up a Virtual Environment in VS Code.

A virtual environment is a Python environment such that the Python interpreter, libraries and scripts installed into it are isolated from those installed in other virtual environments, and (by default) any libraries installed in a “system” Python, i.e., one which is installed as part of your operating system.

We can easily create a Python virtual environment using the following command ,

/yourpythonpath/python -m venv virtual_environment_name

Here I have given the file path for python version 3.8.10 , and the virtual environment is named my_env. This would take a few seconds to create. Once the virtual environment is created you should see a folder with the same name as your virtual environment on the top left corner.

Now lets activate the virtual environment. You can activate your virtual environment using the following command

my_env/Scripts/activate

STEP 02

Install ipykernel

The IPython kernel is the Python execution backend for Jupyter. The Jupyter Notebook and other frontends automatically ensure that the IPython kernel is available. However, if you want to use a kernel with a different version of Python, or in a virtual environment or conda environment, you’ll need to install that manually.

ipykernel can be easily installed using the following pip command ,

pip install ipykernel

STEP 03

Create your Jupyter Notebook

Now you can create your Jupyter Notebook , just like any other file .By adding the .ipynb file extension to the end of your file name VS Code will automatically identify it as a Jupyter Notebook file. Now you have to choose your kernel in which to run this Jupyter Notebook. You can choose this kernel by selecting the select kernel button on the top right corner. Make sure to choose the virtual environment you just created.

There you go !!. Enjoy the flexibility of Jupyter Notebooks and the rich eco system of VS Code at the same time.

--

--