Table of Contents
Last Updated on 2022-07-03 by Kassandra
If you’re using python & Jupyter notebook, at some point you likely have wondered how to add a virtual environment to Jupyter notebook. Up until today, this has never been an issue for me … Until one of my students needed some help.
In this post, I will walk you through the steps to add a virtual environment to your Jupyter notebook.
Up until about February 2022, I had been using Python 3.7 version for most of my trainings & projects. With an upgrade of my laptop, I had to re-setup my system. And what better way to start than with the most recent version of python!
Version 3.10 came out in late 2021, so it’s still a fairly new version. And modules may have a few kinks to iron out still.
Or – they may have changed how they interact with python. For various reasons. And as any good programmer knows, you have to learn to be flexible as well as creative on how to fix things if they go awry.
What You Will Need To Setup
To aide my student, I created a new virtual environment as outlined in my cheatsheet here on my GitHub.
I had already installed virtualenvwrapper-win so I simply followed the next steps.
First, I changed directory into the folder I wanted this new project to reside in. (Meaning a folder in my code parent folder where everything I do for this specific student will be stored.) This step isn’t necessary, since you will be setting the project folder in the next phase. But it is still good practice!
Note that VENVNAME is simply a placeholder – this is what your virtual environment will be called.
mkvirtualenv -p "\Folder Of\PythonVER\python.exe" VENVNAME
In most instances, you likely only have 1 version on your local machine. In that case, you only need to run:
I then set the current folder as the project folder for this virtual environment – which you cannot do without virtualenvwrapper-win!
You now have your virtual environment setup! Simply run the following to turn it off:
How To Add A Virtual Environment To Jupyter Notebook
This next part was a bit tricky, since there were several sites that had differing opinions on this. But this site is what seemed to be the right one. And I will do a video recording for it soon. In the mean time, here are the steps!
While in your virtual environment (because for whatever reason, it wouldn’t work if I did it on my local machine) run the following:
pip install ipykernel
If you are on your local machine, you could run this:
pip install --user ipykernel
… but it wouldn’t work in my virtual environment.
python -m ipykernel install --user --name=VENVNAME
This should spit out something like this for Mac:
Installed kernelspec VENVNAME in /home/user/.local/share/jupyter/kernels/VENVNAME
… or this for PC:
Installed kernelspec VENVNAME in C:\Users\USER\AppData\Roaming\jupyter\kernels\VENVNAME
How To Choose Your Virtual Environment In Jupyter Notebook
When you have your Jupyter notebook running, in the top section is a menu option Kernel > Change Kernel and it is there where you will be able to move between virtual environments.
How To Delete A Virtual Environment Kernel From Jupyter Notebook
To see what kernels are available, run this command:
jupyter kernelspec list
When you confirm the one you want to delete, run this:
jupyter kernelspec uninstall VENVNAME