Skip to content

Adding Virtual Environment to Jupyter Lab

Date: 2021-04-12 16:30:23, by Pc Ng

#JupyterLab #Virtual Environment

Jupyter Lab⚓︎

JupyterLab is the next-generation Jupyter notebook with more comprehensive user interface. The interface is highly modular and customizable.

If you have worked with JupyterLab, you might be realized that the JupyterLab only consists of the virtual environment you created through Anaconda, but those virtual environments created through venv or virtualenv are not found inside the launcher. While IPython kernel is always available in JupyterLab, other version of Python and virtual environment might not available when you start the JupyterLab. That's being said, we got to add the virtual environment to JupyterLab manually.

Setting up Virtual Environment⚓︎

To add the virtual environment, first, make sure that the virtual environment is created and activated. For windows, we can create the virtual environment with PowerShell by typing the following command:

python -m venv env

Next, we need to activate the virtual environment (i.e., the env that we have created).

env/Scripts/activate

Check if ipykernel is installed, we can use pip list to list down a list of installed packages.

.

If ipykernel is not installed, install the ipykernel which provides the IPython kernel for JupyterLab.

pip install ipykernel

Adding Virtual Environment⚓︎

Once it is installed, we can add the virtual environment we created just now to JupyterLab. To add the virtual environment, enter the following command:

python -m ipykernel install --name=env

You should get the following output:

.

The output said that the kernelspec env is installed in the above directory (i.e.,C:\ProgramData\jupyter\kernels\env in this case). Navigate to the said directory, you can see that the folder containing your environment spec.

.

Inside the folder, you will find a kernel.json file containing the information of your virtual environment env.

.

If you open the kernel.json, you should see that it contains the following information:

.

Now, if you open the JupyterLab again, you can find your virtual environment env inside the Launcher

.

Remove the manually added Kernelspec from JupyterLab⚓︎

You can check the list of kernels you have in your JupyterLab with the following command:

jupyter kernelspec list

You should see env inside the list.

.

If you would like to remove the virtual environment env from JupyterLab, type in the following command to uninstall the kernelspec.

jupyter kernelspec uninstall env

.

The output shows that the kernelspec is successfully removed.