Link your Virtual Environment to Jupyter Using Kernels | by mlearnere | Towards Data Science

When I first started using Python, the concept of a virtual environment was extremely elusive to me. It often took me hours of searching and experimenting with creating one only to end up more confused than when I had started.
This article aims to be a one-stop-shop for understanding what virtual environments exactly do, how to create them, and linking them to your Jupyter Notebook.
What is a Virtual Environment?
Create an Environment Using Anaconda
Installing Packages into the Environment
Using Virtual Environments in Jupyter as Kernels
Deleting Environments and Kernels
Let’s first start with the story of Bob.
Bob works at a large financial firm as a Data Scientist. Bob and his team all use Python and regularly collaborate with each other on certain projects. However, since this financial firm is quite large, they all have numerous individual projects they are working on as well. Because of this, there needs to be a universal way to separate these projects from each other to ensure they run on any computer with Python installed. This is where virtual environments come into play.
You can think of a virtual environment as a specific copy of Python in your computer that you can specify yourself. This copy can be any version of Python with any packages installed. Using virtual environments ensures that there are certain barriers between projects. These barriers in place make sure that anyone can run your version of Python regardless of what is on their computer.
I will be using Anaconda in this tutorial as it makes creating and maintaining virtual environments extremely easy. If you do not have it downloaded, you can set up the individual edition on Anaconda’s website. It may take some time to download fully.
If you allow Anaconda to set up a PATH variable, then you can follow what I do on your command prompt. If you do not, you can still follow these steps on your Anaconda Prompt that comes with the download.
Let’s first start by creating a new environment. I will be naming it tutorial, but you can call it whatever you like:
$ conda create -n tutorial python=3.7
Notes:
-n indicates the name of the virtual environment
python=3.7 is optional and can be changed to any version. I prefer setting a specific version when creating the environment as it will give you a “bare bones” Python environment to work within.
You will be asked to proceed with a set of packages to be installed. Type in y and hit Enter.
Now that our environment is created, we are given a prompt by Anaconda.
## To activate this environment, use##     $ conda activate tutorial## To deactivate an active environment, use##     $ conda deactivate
So we will do just that. To activate our environment:
$ conda activate tutorial
This what we should expect to see:
Notice how the name of the virtual environment is in parentheses appeared when we activated it. This is how we know we are inside the environment.
To install packages, we can just use pip as we would for any other packages. Since the aim of this article is to also allow us to use the virtual environment in Jupyter, we will install ipykernel to do the job for us. However, you may install any needed packages for your project at this stage.
$ pip install ipykernel
Let the prompt run and install ipykernel. We can now create Python kernels for Jupyter.
When using Jupyter notebooks, you can either use plain text or code. The code blocks we use are kernels. We can create new kernels with ipykernel.
When we create kernels, we can link it to our virtual environment. This becomes very useful when you are creating a new project and you are unsure of the necessary packages you need. Whether you remove two-thirds of the installed packages or install one more, the kernel will always mirror the virtual environment we created.
To do this:
$ python -m ipykernel install --user --name=tutorial
Now to verify if the kernel is in our Jupyter notebooks:
As you can see, tutorial is now available as one of our kernels. We can now create new notebooks with this environment or utilize existing notebooks with this new kernel.
For management purposes, I will also show you how to reverse what we have accomplished in this article. First, deactivate your virtual environment.
$ conda deactivate
You should see (tutorial) go away.
First, see the list of kernels on your computer. If you have never done this before reading this article, you should expect a list of two kernels: Python 3 and the kernel we just created.
$ jupyter kernelspec list
Now that we know what we have, we know what to remove. To remove the kernel use the command below. You will be asked to verify with y once more.
$ jupyter kernelspec remove tutorial
Once it has completed, verify once more that it has been removed with jupyter kernelspec list.
Removing virtual environments largely follows the same procedure as removing kernels. First, to view the list of environments we have:
$ conda env list
Do not worry if you do not have blind-source-separation in your list. This is one of my projects that I am working on personally. You should expect to see base and tutorial. To remove tutorial:
$ conda env remove -n tutorial
Once it has been removed, view the environment list for verification.
I hope this tutorial helped you understand how to create virtual environments and link them to your Jupyter notebooks. Feel free to save this article to save you time from searching for these procedures again and again.
Thank you for reading.