Why do we use a Virtual Environment in Machine Learning?

Originally published December 27, 2020 on my earlier blog; republished as written.

Article cover by Ryan Blakeney titled Machine Learning with subtitle Why Do We Use Virtual Environments over syntax-highlighted source code on a dark screen

Getting Started

I started learning Machine Learning by using a tutorial on YouTube. In this tutorial, one of the first things we did was create a virtual environment on my computer to run the models and perform the coding in Python. At the time, I just followed the tutorial because I didn't know what I didn't know. After a bit more time, I have started to question some of the things I was taught to understand more of what was happening.

What is a Virtual Environment

Virtual environments are tools that we use to ensure libraries or dependencies that we use for different projects are separated from other projects. This allows us to keep our projects isolated from each other. Some examples of dependencies are: numpy, scipy, matplotlib, pandas, scikit-learn.

You keep these separated to ensure your code is using a version that you know works. An example of thinking about is, if you have an app on your phone that runs excellent, then the app pushes out an update. If your phone or some settings that you had on the app don't match or work with the new version, it will mess up on your phone and cause you to have to fix or relearn how to use the app. I am looking at you Snapchat.

How a Virtual Environment is Created

When you start out learning machine learning, you will learn how to create a virtual environment to put your project inside. The process should follow something like this in terminal/shell:

$ cd $ML_PATH

$ virtualenv my_env

$ source my_env/bin/activate # on Linux or macOS

$ .\my_env\Scripts\activate # on Windows

my_env is the name of your virtual environment. You can change this to whatever you want.

Excerpt From: Aurélien Géron. “Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow.”

← All writing