Installing Virtualenv and Pythonbrew on OS-X

This post will help you get Pythonbrew and Virtualenv installed on OS-X. Two important libraries for Python development.

  • Pythonbrew lets you install multiple python installations without affecting your system’s Python install.
  • Virtualenv lets you set up isolated python installations and modules for each project.

Begin by installing Pythonbrew

# install pythonbrew locally
# do NOT install to your system python directory
curl -kL | bash

Install your desired Python versions

# get a list of available python versions
python list -k

# install desired python
# force the install as python fails some tests at the moment
pythonbrew install <VERSION>
pythonbrew use

# should print out
python --version

# virtualenvwrapper must be installed for each python version
pip install virtualenvwrapper

You can install virtualenv and virtualenvwrapper into the system Python, or into your newly installed python.

# install into system python
# do this if you plan to use the system python as default
sudo pip install virtualenv
sudo pip install virtualenvwrapper

You can install virtualenv and virtualenvwrapper into the system Python, or into your newly installed python.

# install into pythonbrew installed python
# do this if you want to over-ride the default python installation
pythonbrew switch <VERSION>
pip install virtualenv
pip install virtualenvwrapper

Add support for Pythonbrew to bash by adding the following to the end of your ~/.bashrc

Follow this guide if you want to make your .bashrc modular.

# Python definitions

# Pythonbrew
# add pythonbrew support
if [[ -s $HOME/.pythonbrew/etc/bashrc ]]; then
source $HOME/.pythonbrew/etc/bashrc

Add virtualenvwrapper support to ~/.bashrc

# Python definitions

# Virtualenvwrapper
# virtualenv wrapper support
export WORKON_HOME=~/Workspace/VirtualEnvs

if [[ -s /usr/local/bin/ ]]; then
source /usr/local/bin/

# tell pip to only install inside virtualenvs

# make pip use the virtualenv dir

# add pip bash completion
# use eval to avoid the error "Could not find an activated virtualenv (required)."
eval `pip completion --bash`

Close the terminal and re-open it to reload your .bashrc.

When creating Virtualenv environments, virtualenv will use the currently set Python install.

If you wish to use a Python install other than the current system install, run the following command before running mkvirtualenv.

pythonbrew use <VERSION>

Note: I’ve found that virtualenvwrapper has stopped obeying this. To force virtualenvwrapper to install a specific version, do the following.

pythonbrew use <VERSION>
MY_PYTHON="$(command which python)"
mkvirtualenv -p $MY_PYTHON <NAME>

Some basic Virtualenvwrapper commands:

  • mkvirtualenv PROJECTNAME – create a new virtualenv project.
  • workon PROJECTNAME – enter the virtualenv for the project.
  • deactivate – stop working on the current virtualenv project.
  • cdvirtualenv – change to the directory of the current virtualenv project.

If you need to add environment variables to your project, edit the ‘bin/postactivate’ file inside the virtualenv directory. This file is executed when the ‘workon’ command is run and can be used to add more paths to $PYTHONHOME and other useful commands.


5 Responses to “Installing Virtualenv and Pythonbrew on OS-X”

  1. […] Twisted Pair Development Two peoples adventures in the world of game development « Installing Virtualenv and Pythonbrew on OS-X […]

  2. […] I googled around and found a solution at: TwistedPairDevelopment […]

  3. that is a fine tutorial.
    with your setup when you use te command pythonbrew use does the selected version persist when openning a new shell session?

    • forget it. found the switch command

    • I believe the ‘set’ command does what you are after.
      Personally, I would be careful with this. You never know what script is going to expect the default system Python install and end up in a different install.
      Perhaps there are no problems, I just rather avoid the possibility of any issues.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: