Introduction:

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?

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.


Create an Environment using Anaconda

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.

#programming #data-science #python #tutorial #virtual-environment

Link Your Virtual Environment to Jupyter with Kernels
1.05 GEEK