Python Virtual Environments: Setup & Usage

Virtual environments enable you to manage multiple programs with different dependencies.

Table of Contents

Why Use Virtual Environments?

Python includes Pip, a robust packaging system to install and manage dependencies not part of the standard Python library.

The verdict is still out on what Pip stands for – but it’s likely “Pip Installs Packages”, “Pip installs Python”, or “Preferred Installer Program”

If there’s a “Pretty Immense Problem” with Pip is that it installs packages into your global environment by default. Installing everything into your global environment enables every program to access installed packages.

Python Pip Package Manager

But what happens if you are working on multiple projects that require different versions of a package? You’re going to have lots of headaches due to version conflicts.

The good news is that virtual environments solve the challenge. Virtual environments, or “virtual envs” for short, create an environment that’s isolated, allowing you to keep each project’s dependencies separate. Typically, you make one virtual environment for each project.

And as of Python version 3.3, there’s nothing additional we need to do as virtual environments are built into the standard library.

What’s a Virtualenv?

A virtualenv isn’t complex – it’s just a directory. This folder contains software that ensures your code uses the correct Python interpreter and site-packages. You can think of it as a “sandbox” for your code.

You should never add any of your project files to this directory, as you don’t commit virtualenvs to a git repository.

Instead, we export a list of the packages and their versions in the virtual environment into a text file, which is conventionally named requirements.txt. With the requirements file, anyone can easily recreate your environment.

Detecting The Current Environment

You can type which pip3 to determine the current environment. Which will return a path to your virtual environment or a system directory depending upon which you’re using.

Python Which Command

Creating a Virtual Environment

You can create a virtual environment by typing python3 -m venv venv.

Create Python Virtual Environment

Activating a Virtual Environment

We need activate the virtual environment in order to use it. You can do this by typing source ./venv/bin/activate

Activate Python Virtual Environment

When your virtual environment has been activated, you’ll notice the name of the virtual environment prepended as shown below:

Python Virtual Environments

As shown in the above, we can determine what virtual environment we’re using by typing which python3 or which pip3.

Saving Your Virtual Environment

At some point, we’re going to want to save our virtual environment. This folder can quickly grow very large, so we don’t want to commit this to a respository. Instead, we’ll create a “requirements.txt” file to save all of the packages and their versions to upload to our source control software.

pip freeze > requirements.txt

Pip Freeze

Deactivating Your Virtual Environment

Deactivating a virtual environment is easy. You simply type deactivate at the terminal.

Deactivate Python Virtual Environment

How to Remove a Virtual Environment

Deleting a virtual environment is easy. All you need to do is remove the directory. Remember, don’t put any of your project files into the venv. It’s to be thrown away and rebuilt – after exporting the latest requirements.txt.

Python Removing a Virtual Environment

Also, remember to deactivate your virtual environment after you remove it. If you don’t, you will try to use a non-existent python instance and it’ll give you errors.


Leo Smigel

Based in Pittsburgh, Analyzing Alpha is a blog by Leo Smigel exploring what works in the markets.