The open blogging platform. Say no to algorithms and paywalls.

How to Add Your Virtual Environment to the Jupyter Kernel in Windows

A tutorial on adding a virtual environment to the Jupyter kernel in Windows.

Managing conflicting dependency for multiple projects using a single Python package installed system-wide is troublesome. Having a separate virtual environment for every project is nice, but requires lots of space. Having a master virtual environment definitely combine the best of the two worlds. Being able to choose an environment as a kernel when creating a new notebook is the cherry on top. Here, I will show you how to add your virtual environment to the Jupyter kernel in Windows.

Install Jupyter

First, install JupyterLab or Jupyter Notebook from your main Python installation.

To install JupyterLab (recommended):

pip install jupyterlab

To install Jupyter Notebook:

pip install notebook

Install virtual environment manager

Here, I’m using virtualenv that can be installed using:

pip install virtualenv

Other virtual environment managers such as venv also could be used. See this StackOverflow answer for more details.

💡 Learn how to configure the VS Code environment for working with Python using WSL from Microsoft:

Work with Python in VS Code using WSL from Microsoft

👉 To read more such acrticles, sign up for free on Differ.

Create virtual environment

Python in Windows is distributed with py the launcher that supports multiple Python versions. You can specify the Python version used in the virtual environment by typing the version as follow:

py -3.8 -m virtualenv env

Here, the envis the name of the virtual environment folder.

Starting virtual environment

To start the virtual environment from Windows PowerShell (VSCode default terminal):

env\Scripts\Activate.ps1

To start the virtual environment from Windows Command Prompt:

env\Scripts\activate

To close the virtual environment:

deactivate

Add virtual environment to the Jupyter kernel list

To add a virtual environment to the Jupyter kernel list, we need to install ipykernel from inside the environment variable (after activating the environment) first:

install pip install ipykernel

Then, add the virtual environment with your preferred name to identify the virtual environment:

py -m ipykernel install --name “py3.8-env”

Check your kernel list

Test that you correctly add your environment to the Jupyter kernel list with the below code:

jupyter kernelspec list

Alternatively, just start your Jupyter and see whether you are able to choose your environment as a kernel option or not.

That is all for the tutorial on how to add your virtual environment to the Jupyter kernel in Windows. Don’t forget to use IPython %magic (not the !bang) while using operating system operations such as:

%pip list

Have a nice day~




Continue Learning