Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save duonghuuphuc/7939cfbf82d9664274d299fff3d4c205 to your computer and use it in GitHub Desktop.
Save duonghuuphuc/7939cfbf82d9664274d299fff3d4c205 to your computer and use it in GitHub Desktop.
Install virtualenvwrapper on macOS 10.15 and later

Install virtualenvwrapper on macOS 10.15 and later

At WWDC 2019 on June 3, 2019, starting with macOS Catalina (10.15), the macOS will use zsh as default shell across the operating system. Besides, Bash is still available along with the new zsh; however, by setting zsh as the default shell, Apple does force you to move to the new platform.

virtualenvwrapper is a set of extensions to the original virtualenv which is a tool for creating isolated virtual Python environments. The extensions include wrappers for creating and deleting virtual environments and otherwise managing your development workflow, making it easier to work on more than one project at a time without introducing conflicts in their dependencies. According to the virtualenvwrapper docs, there are some noteworthy features such as:

  • Organizes all your virtual environments in one place.
  • Wrappers for managing your virtual environments (create, delete, copy).
  • Use a single command to switch between environments.
  • Tab completion for commands that take a virtual environment as argument.
  • User-configurable hooks for all operations.
  • Plugin system for creating more shareable extensions.

With the new zsh shell on macOS 10.15 and later, the process of installing virtualenvwrapper may have some differences. In this short tutorial, I will show you how to install virtualenvwrapper on macOS 10.15. Before beginning this tutorial, if you don't feel comfortable with command-line tools such as virtualenvwrapper, you can use Anaconda which does also support creating and manipulating virtual Python environments, and support GUI interaction.

1/ Install virtualenvwrapper

The virtualenvwrapper is a wrapper of the original virtualenv, we thus need to install both on the system, by executing the following command on Terminal:

pip install virtualenv virtualenvwrapper

2/ Config virtualenv and virtualenvwrapper

This step is to define the default path for the virtual environment. First, we need to create a directory where we store all virtual Python environments by executing the command: mkdir ~/.virtualenvs. Then, we add virtualenv variables to the .zshrc file by performing the following steps:

  • Open or create a zsh profile: nano ~/.zshrc
  • Insert the following lines of code:
# Configuration for virtualenv
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3
export VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/bin/virtualenv
source /usr/local/bin/virtualenvwrapper.sh

Although virtualenvwrapper is tested under Python 2.7 to 3.6, you should install virtualenvwrapper on Python 3 environment. To install and make Python 3 as the default Python version on your system, you can refer to this tutorial.

3/ Some useful commands

virtualenvwrapper consists of a set of commands, but you should know the following common commands:

  • Create a new virtual environment: mkvirtualenv <env_name>
  • Start and work on a virtual environment: workon <env_name>
  • Deactivate (exit) a started/activated virtual environment: deactivate

4/ Further reading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment