Mac OS X Lion: The perfect setup for Python / Django

If like me, you are developing Django applications on a Mac, you will probably face a few issues when upgrading to Lion. In my case, I haven't even upgraded, I decided to wipe it all out and start from a fresh installation (my MacBook hard disk did thank me for it!).

So here we go, I've put down the stepped I have followed to get my setup back to business.

First of all, Mac OS X Lion now ships with Python 2.7 already installed which is the version we will use.

guillaumepiot$ python -V
Python 2.7.1

Also, you will need some development libraries to be installed, for that download XCode from the App Store. Then browse to your Applications folder and install XCode. Yes it's a bit silly that it doesn't install itself automatically, I got caught the first time!

Update: When installing Xcode, you may have to install the command line tools as well.

Then, you will need to install the command line tools. For that, you must launch Xcode, go to "preferences > downloads" and click install command line tools.

Install MySQL

Download the MySQL installation package:

Mac OS X ver. 10.6 (x86, 64-bit), DMG Archive

Install the app as you normally would, double-click the DMG file and follow the instructions.

I recommend to install the 3 items: the main package, the startup item and the preference panel widget (for added control in your System Preferences).

Once it's all installed, go to your System Preferences, go to MySQL, and start the server.

Setup MySQL users:

First, we want to reset the root password.

mysqladmin -u root password NEWPASSWORD

Then, I like to create a personal user for connecting locally (rather than connecting with root).

Start MySQL:

/usr/local/mysql/bin/mysql -u root -p

(Then enter your root password)

Create a new user and assign it all privileges on all databases, or on a specific database (up to you!).

mysql> CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'username'@'localhost';

Tips: I like to use Sequel Pro to manage my database and users, rather than using the command line. If you are re-intalling Sequel Pro, you may want to copy and move you favourites from that path, from one machine to another: ~/Library/Preferences/

Install the Python libraries

Install Setup Tools:

First, we'll need Setup Tools to help us installing the next packages. Download Setup Tools.

Un-compress, cd /to folder, and run:

sudo python install

Install Django:

sudo easy_install django

Verify that python is installed properly:

import django

Install MySQLdb:

Download the package here: MySQL-Python

Untar the package, then we'll need to edit the config file to add the right path to MySQL bin. Edit site.cfg and change the following:

# The path to mysql_config. 
# Only use this if mysql_config is not on your PATH, or you have some weird 
# setup that requires it. 
mysql_config = /usr/local/mysql/bin/mysql_config

Then, cd /to folder, and run:

sudo python install

Once it's installed, try to load it:

import MySQLdb

It's likely you will come with the following error:

Library not loaded: libmysqlclient.18.dylib

Here's how I fixed it, add the following to your ~/.profile or ~/.bash_profile:

vi ~/.profile (to edit the file)

Insert the following line:

export DYLD_LIBRARY_PATH=/usr/local/mysql/lib:$DYLD_LIBRARY_PATH

Save the file and then re-open a new Terminal window for the changes to take effect!

That should be fixed!

What next?

Well that's it for the basic, you can now run a simple Django App (like my Django Poll App for example!)

If you need more packages installed, then proceed as usual.

Good luck!

< / >