Python & MySQL on MAC OS X

Using Django.

Get MySQL working with Python on Mac OS X Mavericks. The error:

raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb

I had previously used Pip to install PyMySQL, soon realised this was a mistake, so uninstalled that, and installed https://pypi.python.org/pypi/MySQL-python/1.2.4 instead:

Download the archive, extract it, cd into it, then run:

python setup.py build

and then run (sudo if you are not root of course)

sudo python setup.py install

Now you’ll get a different error:

raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
...
Reason: image not found

We need to set an environment variable to use this library.

Add this to ~/.bash_profile:
export DYLD_LIBRARY_PATH=/usr/local/mysql/lib/

Then source it:
source ~/.bash_profile

Try syncing the db, and running the server again:

python manage.py syncdb
and
python manage.py runserver

All is now good (or should be). Back to writing some actual code!

Don’t forget to actual import MySQLdb!

import MySQLdb

Leave a Reply

Your email address will not be published. Required fields are marked *