First, allow me to make a few assumptions:

  1. You're running Mac OS X 10.8.
  2. You have MAMP 2.x installed (and this is preventing MySQL-python from installing).
  3. You're going to run your Django site within a virtualenv (If you weren't planning on doing this, now's a great time to change your mind!).
  4. You're tired of PHP and you want to use Django, but because of other projects are unable to remove MAMP and start fresh.

Why it's failing:

MySQL-python won't compile because it needs the MySQL headers which are not in the expected location under a MAMP install. Luckily, this isn't hard to remedy.

Step 1 - Prepare the Virtual Environment


sudo easy_install virtualenv

Virtualenv is the tool used to partition off one Python project from another, giving them isolated python paths for dependecny installation. That way, if you're a normal developer and are thus juggling multiple projects simultaneously, they won't clobber each other's packages. And as any guy will tell you, you hate to see your package get clobbered.

It would also serve us well to have a centralized location for our virtual environments. I propose ~/virtualenvs. Create it with mkdir ~/virtualenvs.

Step 2 - Fix the Mountain Lion bug

If you're a huge jerk and are still using Lion, skip to Step 3.

As of this writing, Mountain Lion broke Python and virtualenv, as per this link. You need to follow each of the steps it outlines, including downloading the Mountain Lion developer command line tools, installing pythonbrew, and installing Python 2.7.2 using pythonbrew. If you don't do this, you won't be able to make any virtualenvs.

Step 3 - Finish the virtualev

With these commands behind us, it's time to create our specific virtualenv. It makes sense to name it after the project it will service (obviously), so we'll call this one "myproject".

Run: virtualenv ~/virtualenvs/myproject and you should see this output:

virtualenv ~/virtualenv/myproject
New python executable in /Users/[myuser]/virtualenvs/myproject/bin/
Installing setuptools......done.
Installing pip......done.

Lastly: activate the virtualenv by running this:

source /Users/[myuser]/virtualenvs/myproject/bin/activate

If it worked, your terminal will look something like this:


Step 4 - Get GCC

You need GCC to compile those missing MySQL headers. If you have Xcode installed, you're good to go. If you don't and you don't want to download the whole 5 million gigabyte file, install just GCC from here.

Step 5 - Get some Homebrew'd lovin:

If you're currently floating through life without Homebrew, install it now by running:

ruby -e "$(curl -fsSL"

Step 6 - Download MySQL:

Yep, download it again. You've already got it running under the MAMP umbrella, but we need to place it in the normal location as well. It's important to do this step with Homebrew because it downloads the required header files, too. Run:

brew install mysql

You won't ever need to activate or run this MySQL version ever. Just let it sit there. I advise always using your MAMP MySQL to power each Django project you build.

Step 7 - Setup your Django requirements

Now our areas are mostly set up, but we still haven't installed Django. Here, you have a few options. If you're checking out an existing project... do that. If you're starting fresh you'll need to install Django locally to run: startproject myproject

Either way, within your project, run these commands:

mkdir requirements/
touch requirements/base.txt

Populate base.txt with these lines:


You'll add a bunch more packages later, but that's all for now. Now run:

sudo pip install -r requirements/base.txt

This is the big moment of truth. If you did everything right, Django and MySQL-python will both install correctly and you'll be able to resume Pythoning to your heart's content (which should involve an unfathomable amount of Pythoning). If you need more assistance continuing with Django, I highly suggest this tutorial.

Step 8 - GTFO of here

You're up and running now so get on with building the website of your dreams. Make that buck!

comments powered by Disqus