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.
Run:
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
.
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.
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:
(myproject)username:bin$
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.
If you're currently floating through life without Homebrew, install it now by running:
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
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.
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:
django-admin.py startproject myproject
Either way, within your project, run these commands:
mkdir requirements/
touch requirements/base.txt
Populate base.txt with these lines:
Django
MySQL-python==1.2.4
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.
You're up and running now so get on with building the website of your dreams. Make that buck!