Setting up your Python environment with virtualenv and virtualenvwrapper on MacOS

Virtualenv is a great tool for creating isolated Python environments that contain all the necessary dependencies for a particular project. Without them, our global environment can quickly become out of sync with what we expect for a particular project.

In today’s post, I’ll walk you through my preferred flow for setting up Python and virtual environment on a new MacOS environment.

1. Install brew 🍺

brew is a package manager for macOS. It’s awesome.

You may already have it. If you do, the following command should work

$ brew --version
Homebrew 2.2.15

If not, install it with this command:

/usr/bin/ruby -e "$(curl -fsSL"

2. Install Python 🐍

With brew setup, we can now use it to install Python.

brew install python3

If everything installed correctly, you should be able to see the following:

$ which python3

3. Install virtualenv and virtualenvwrapper 📦

With Python installed, we should have access to the pip package manager.

Let’s install two packages:

  • virtualenv — a tool for creating isolated virtual python environments
  • virtualenvwrapper — extensions to help create and delete environments
pip3 install virtualenv virtualenvwrapper

4. Set up path and virtualenv variables 🗒

We need to add some user configuration settings, so that our system knows where to find Python and our custom configurations for virtualenv. What this means is adding some lines to our .zshrc or .bash_profile.

In this example, we’ll edit our zsh configuration file with nano line text editor.

nano ~/.zshrc

Add the following lines:

# Path setup
export PATH=/usr/local/share/python:$PATH
# virtualenv configuration
export WORKON_HOME=$HOME/.virtualenvs export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3 export VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/bin/virtualenv
source /usr/local/bin/

Now, close and re-open your terminal window.

5. Using virtualenvwrapper 🚀

With a new terminal open, we should be good to go.

We can create a new virtualenv with mkvirtualenv

mkvirtualenv my-awesome-project

We can now install dependencies in their own isolated environment.

$ (my-awesome-project) pip3 install -r requirements.txt

To exit our virtualenv, we can use deactivate

$ (my-awesome-project) deactivate

To enter in to our virtualenv, we can use workon

$ workon my-awesome-project
$ (my-awesome-project)

To list all virtualenvs, we can use lsvirtualenv

$ lsvirtualenv

Finally, to delete a virtualenv, we can use rmvirtualenv

$ rmvirtualenv my-awesome-project
Removing my-awesome-project...



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Chanon Roy

Hello. I write primarily about programming and technology. I’m also fluent in film references.