Skip to content

Environment management with `virtualenv`

Daniel Khashabi edited this page Sep 19, 2017 · 1 revision

Environment management with virtualenv

One way to set up your python development environment on OS X is to use virtualenv. This allows you to have separate installations of python packages for various development scenarios and to develop using both python 2 and python 3 as needed.

Basic pattern: Use brew to install python. Use the pip installed by brew to install python packages that should be globally available. Use pip within a virtualenv to install python packages within that virtualenv.

  1. Install python and virtualenv:
brew install python
brew install python3
pip install virtualenv

Q: Why install python 2 when it comes bundled with the os?
A: It is simple to keep the version you install up to date with brew. Also, apparently Apple has made their own changes to the bundled python, so you really don't want to rely on it to behave like python on other systems. Also apparently, upgrading OS X can mess up the bundled python.

  1. To prevent you from accidentally installing a python package outside of a virtualenv unless you really mean to, update your ~/.bashrc with the following lines:
# Only install python packages in virtualenv
export PIP_REQUIRE_VIRTUALENV=true

# Unless you really want to install python package globally
gpip(){
   PIP_REQUIRE_VIRTUALENV="" pip "$@"
}
  1. Reload to get the updates:
source ~/.bash_profile
  1. Create a directory for your virtual environments. Create this directory as a peer of your development workspace, e.g. your 'git' or 'projects' directory. The rest of this document will assume that your project directory is ~/git and your virtual environment directory is ~/venvs.
mkdir ~/venvs
  1. Create and activate some virtual environments to understand how it works:

To create and activate a python 2.7 virtualenv named py2-scripts:

cd ~/venvs
virtualenv --prompt py2-scripts- py2-scripts
source ~/venvs/py2-scripts/bin/activate

To create and activate a python 3 virtualenv named py3-proj:

cd ~/venvs
virtualenv --python python3 --prompt py3-proj- py3-proj
source ~/venvs/py3-proj/bin/activate

To leave an active virtualenv:

deactivate

Optional steps to make life better

  1. Install virtualenvwrapper. Be sure to run this command outside of any virtualenv:
deactivate
gpip install virtualenvwrapper
  1. Add the following lines to your ~/.bashrc:
# virtualenvwrapper
export WORKON_HOME=$HOME/venvs
export PROJECT_HOME=$HOME/git
source /usr/local/bin/virtualenvwrapper.sh
  1. Reload ~/.bashrc:
source ~/.bashrc
  1. List your virtualenvs:
workon
  1. Activate an existing virtualenv and associate a project directory with it. Assuming that your project directory for the py2-scripts virtualenv is ~/git/some-old-scripts:
workon py2-scripts
cd ~/git/some-old-scripts
setvirtualenvproject

Now, when you workon py2-scripts, your shell will automatically change to your project directory.

  1. Associate a project directory with your other virtualenv:
deactivate
setvirtualenvproject ~/venvs/py3-proj ~/git/new-cool-project
  1. Going forward, you can create a virtualenv and a project directory with the same name at the same time:
mkproject [virtualenv opts] myproject

Or create a virtualenv for an existing project:

mkproject -f [virtualenv opts] myproject

Resources