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 http://xrl.us/pythonbrewinstall | 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
# https://twistedpairdevelopment.wordpress.com/2012/01/16/installing-python-with-pythonbrew-on-mac-os-x
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
fi
Add virtualenvwrapper support to ~/.bashrc
#-------------------------------------------------------------
# Python definitions
#-------------------------------------------------------------
# Virtualenvwrapper
# virtualenv wrapper support
export WORKON_HOME=~/Workspace/VirtualEnvs
if [[ -s /usr/local/bin/virtualenvwrapper.sh ]]; then
source /usr/local/bin/virtualenvwrapper.sh
fi
# PIP
# tell pip to only install inside virtualenvs
export PIP_REQUIRE_VIRTUALENV=true
# make pip use the virtualenv dir
export PIP_VIRTUALENV_BASE=$WORKON_HOME
# 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.