We will be setting up VirtualEnv first so we can easily create dev environment. If you don’t know what VirtualEnv is go here
After that we will install MySql and set up our requirements file so we can run pip to install mysql python driver and django.

Most of this content was gathered from different sources. Please see the end of the article for references.

Python site-packages

Managing Python packages is not the easiest thing in the world. Installing is not so bad but them when we have to delete that things become messy if we are not careful. First thing we are going to do is install Distribute. Distribute is a fork of Setuptools/easy_install that aims to be more reliable with less bugs.
So If I decide to install a python package but later I want to delete it, I can just drag and drop to trash without issues. We will be setting up  the following path to hold our python packages “~/.local”

Let’s install Distribute:

mkdir -p ~/.local/lib/python2.7/site-packages
wget http://pypi.python.org/packages/source/d/distribute/distribute-0.6.28.tar.gz
tar -xzf distribute-0.6.28.tar.gz
cd distribute-0.6.28
python setup.py install --prefix=~/.local

Because we have installed in “~/.local” we need to add some locations to the PATH and PYTHONPATH environment variables

vim ~/.bash_profile

Add a few lines

# Path ------------------------------------------------------------
if [ -d ~/.local/bin ]; then
  export PATH=~/.local/bin:$PATH

# Python path -----------------------------------------------------
if [ -d ~/.local/lib/python2.7/site-packages ]; then
  export PYTHONPATH=~/.local/lib/python2.7/site-packages:$PYTHONPATH

# Load in .bashrc -------------------------------------------------
if [ -f ~/.bashrc ]; then
  source ~/.bashrc

Now let’s reload our profile:

touch ~/.bashrc
source ~/.bash_profile
Now let's check that we are running from the correct place:
which easy_install

Assuming the response is ~/.local/bin/easy_install, then we’re all set!


“Python packages installed to ~/.local are indeed local to the user, but they are also global in the sense that they are available across all of a given user’s projects. That can be convenient at times, but it also creates problems. For example, sometimes one project needs the latest version of Django, while another needs Django 1.3 to retain compatibility with a critical third-party extension. This is precisely the problem that virtualenv was designed to solve.” On my systems, virtualenv, virtualenvwrapper, and Git are the only Python packages that are always available — every other package is confined to its virtual environment.

Let’s install virtualenv and virtualenvwrapper:

easy_install --prefix=~/.local virtualenv virtualenvwrapper

We’ll then open/create the ~/.bashrc file:

vim ~/.bashrc

and add some lines to it:

# Turn on advanced bash completion if the file exists
if [ -f /usr/local/etc/bash_completion ]; then
  . /usr/local/etc/bash_completion

# Locate virtualenvwrapper binary
if [ -f ~/.local/bin/virtualenvwrapper.sh ]; then
    export VENVWRAP=~/.local/bin/virtualenvwrapper.sh

if [ ! -z $VENVWRAP ]; then
    # virtualenvwrapper --------------------------------------
    # make sure env directory exists; else create it
    [ -d $HOME/sites/env ] || mkdir -p $HOME/sites/env
    export WORKON_HOME=$HOME/sites/env
    source $VENVWRAP

    # virtualenv ---------------------------------------------

    # pip ----------------------------------------------------
    export PIP_DOWNLOAD_CACHE=$HOME/.pip/cache

Let’s re-load our bash environment again:

source ~/.bash_profile

Let’s add some customization to the postactivation script, which defines what happens after activating a virtual environment:

vim ~/sites/env/postactivate

Add the following lines:


# Add the active project to the PYTHONPATH
if [ -d ~/sites/env/$proj_name/lib/python2.7/site-packages ]; then
  add2virtualenv ~/sites/env/$proj_name/lib/python2.7/site-packages

# "cd" into the virtualenv, or its "project" folder if there is one
if [ -d ~/sites/env/$proj_name/project ]; then
  cd ~/sites/env/$proj_name/project
# I'm using sublime text2, so I like to open my project right at the same time
  subl ~/sites/env/$proj_name/project
  cd ~/sites/env/$proj_name
  subl ~/sites/env/$proj_name

Now we have virtualenv and virtualenvwrapper installed and ready to create new virtual environments, which will be stored in ~/sites/env/.

Creating virtual environments

Let’s create a virtual environment called “testingproject”:

mkvirtualenv testingproject

That should create a new virtual environment and automatically switch our present working directory to that new testingproject environment.

In the future, when you want to work on a project contained in a virtual environment, use the “workon” command followed by the name of the virtual environment:

workon testingproject

That will activate the “testingproject” virtual environment, and if you’ve made the changes I recommended earlier to the “postactivate” script, you should be taken directly to the virtual environment’s directory at ~/sites/env/testingproject. If not, you can always get there via the “cdvirtualenv” command, which will take you to the currently-active virtual environment’s home.


To make django and python work with mysql we will need to install MySQLdb package. Unfortunately we have to compile it and to do so we need to do a full installation of MySQL server on Mac OS Lion. After installing and compiling everything you can use MAMP if you want.

If you want to use MAMP go to step 2, otherwise keep reading

1) Go to dev.mysql.com/downloads/ and download to latest version. I got mysql-5.5.25-osx10.6-x86_64.dmg and followed the instructions for the installation. If you set the preferences correctly in Startup Package and Preferences Pane (double click MySQLStartupItem.pkg and then MySQL.prefPane), you can configure mysql to be started/stopped from System Preferences.

2) Let make a hard copy of MySql so we can have all the dependencies to compile MySQLdb.
Dowload the tar.gz from dev.mysql.com/downloads/, than in the command line:

tar -xvzf mysql-5.5.25-osx10.6-x86_64.tar.gz
sudo cp -r mysql-5.5.25-osx10.6-x86_64 /usr/local/
sudo mv -r /usr/local/mysql-5.5.20-osx10.6-x86_64 /usr/local/mysql

Add this to your .bash_profile, so that your system will know where to find mysql related files:

cd ~
vim .bash_profile
export PATH=$PATH:/usr/local/mysql/bin #add this line
export DYLD_LIBRARY_PATH=/usr/local/mysql/lib #add this line

This is it for MySQL for now.

Final touchs

Now we are going to set up our requirements.txt document with all the packages that we need and pip will take care of downloading and compiling our virtual environment

workon testingproject
mkdir project
vim project/requirements.txt

Add the following lines:


First line specify python version that you need. Than Django and MySql-python (MySQLdb) package.
Save the file and run

pip install -r project/requirements.txt

It will install Python and Django but you going to see an error message like this for MySQL-python:

EnvironmentError: mysql_config not found

It’s ok. We are going to fix this. We will notice that pip created a folder called “build”. Let change one file to make it work.

cd build
cd MySQL-python
vim site.cfg

change the mysql path as follows

mysql_config = /usr/local/mysql/bin/mysql_config

Save and close, then run the pip command again.

pip install -r project/requirements.txt

Now everything should work and you should be able to to “import MySQLdb” from inside you python shell.

*If you want to make django work with MAMP, you can just change the HOST inside settings.py:

'HOST': '/Applications/MAMP/tmp/mysql/mysql.sock'