Using virtual environments is a very good idea when working on Python projects.
Using Jupyter Lab desktop is a very good idea when working on data science projects.
Using Jupyter Lab desktop with virtual environments is an especially good idea. Unfortunately, the documentation for how to do this is rather lacking.
After some messing around, I was able to load up a virtual environment for a Python project from within Jupyter Lab desktop by doing the following:
-
Load up the virtual environment for a given project. (e.g.,
source venv/bin/activate
. See my post on virtual environments for more details) -
Install
ipykernel
within the virtual environment with the following command:pip3 install ipykernel
. -
From within the virtual environment, create a link to the virtual environment:
python3 -m ipykernel install --user --name=<some name for the virtual environment kernel>
. I prefer using names with no spaces. -
Now, fire up Jupyter Lab and load up a notebook. From the kernel dropdown menu, you should be able to pick the kernel associated with the virtual environment created in the previous step. Selecting the virtual environment kernel should give you access to the project’s virtual environment, including the project’s particular Python variant and associated modules.
To examine the list of installed kernels, run jupyter kernelspec list
from the command line.
To delete any specific kernel, run jupyter kernelspec uninstall <kernel name>
.