If you are new to programming with Python, you may have heard mention of something called a Virtual Environment. This may just sound like one more obstacle standing in your way of writing super awesome Python code. You may be wondering if you really have to set up a virtual environment before you begin a project. The short answer is no. You do not have to set up a virtual environment before you start writing Python code. However, it is a pretty good idea to go ahead and do it as it is really useful and not that hard to do.

The Pros

A quick google search will provide you with several resources that explain why it is a good idea to use a virtual environment, but basically, it comes down to compatibility. We’ve all experienced a situation with our computers where we have updated an application or our operating system, and suddenly something else no longer works. This can happen with the programs you write. You may write code that uses a specific library, and then later, you need to use a newer version of the library. After installing an update to the library, your old code no longer works. That’s a pretty big bummer. By using virtual environments, you can update libraries for one project without updating them for others. It helps to ensure that working code stays working.

The Cons

The cons to using virtual environments are not too serious, but they do exist. The main con is that you have to remember to enable the correct virtual environment (more on this later) before you resume working on your project. Also, you will have to remember to install the same package over and over in each new virtual environment in order to use it. This also takes up hard drive space as you will be installing the packages in more than one spot on your computer. However, these cons are rather marginal compared to the benefits of using a virtual environment.

How to Set Up a Virtual Environment

Background

Actually setting up a virtual environment may or may not be straightforward. The exact commands and steps will depend on what version of Python you are using and how you have it configured. In the following example, I am using Python version 3.7.6. First, I make sure pip3 is up-to-date with:

pip3 install --upgrade pip

It is also worth noting that I am using the bash shell (zsh is now the default for Macs). With this in mind, the most straightforward way to create a virtual environment is to do the following:

Setting Up a Virtual Environment

1). Install the virtualenv tool by opening up Terminal (Applications > Utilities > Terminal) and running the following command:

pip3 install virtualenv

2) Create a new virtual environment by running the following command. You can replace envName with whatever name you want to give the new virtual environment:

virtualenv envName

3) Activate your new virtual environment by running the following command. Again, replace envName with whatever name you have given your new virtual environment:

source envName/bin/activate

4) To deactivate the virtual environment, run the following command:

deactivate

Understanding What Just Happened

In Step 1 above, you are using pip, a package-management system written in Python to install the software package virtualenv. Because I am dealing with Python3, I used the pip3 command. In Step 2, we use the virtualenv tool to create a new virtual environment named envName. You may be wondering, what does that even mean? This command creates a new folder called envName and puts a unique copy of Python in that folder. In my case, I had opened Terminal from its default directory, which is my home folder. So in Step 2, the new folder was created in my home directory. Looking at the screenshot below, you can see that inside the envName folder is a folder called bin. Inside of bin there is a file called activate. In Step 3, you use the source command to run the code located in the activate file.

Figure 1 – When you create a new virtual environment, a folder is created with the name you gave it. This folder contains an new instance of Python.

You can tell that Step 3 worked if your command prompt changes to include the name of the virtual environment in parentheses as shown below:

Figure 2 – You can see that I am currently working within the virtual environment that was activated with the source command.

When the virtual environment is activated any packages that you install will be installed in this new folder structure and will only be accessible when you are working within the virtual environment.

Conclusion

Virtual environments are very useful and you should probably get use to the idea of creating one for each of your projects. What was covered above are the basics of how to do this. Things can get a little more complicated depending on how you have everything set up.

If you run into problems, comment below and I’ll see if I can help you out.