Installing Django with MySQL on Mac OS X

Posted by: barbara | Date: Jun 04, 2008 | Updated: Aug 11, 2008 | Category: Django Mac OS X MySQL Python    

Django's official installation instructions are here:

http://www.djangoproject.com/documentation/install/

However, they get a little vague around the section titled "Get your database running".

I followed this post and, aside from a few missing notes, got the Django/MySQL install done in about 15 minutes:

http://antoniocangiano.com/2007/12/22/ how-to-install-django-with-mysql-on-mac-os-x/

Installing Django

I followed the convention this blogger used and just put the Django install in my user directory, under a folder called "Code":

/Users/myusername

mkdir Code

cd Code/

svn co http://code.djangoproject.com/svn/django/trunk django_trunk

To tell Python where to find Django:

cd /Library/Python/2.5/site-packages/

vi django.pth

Paste this single line:

/Users/myusername/Code/django_trunk

And add it to your path:

vi .bash_profile

PATH=$PATH:$HOME/bin:/Users/myusername/Code/django_trunk/django/bin

Installing MySQL

Download MySQL-python-1.2.2.tar.gz to your home directory (/Users/yourusername/) - get it here:

http://sourceforge.net/project/showfiles.php?group_id=22307&package_id=15775&release_id=491012

Untar and switch to the MySQl folder:

tar xvfz MySQL-python-1.2.2.tar.gz

cd MySQL-python-1.2.2

Edit _mysql.c lines 37, 38 and 39 as follows:

//#ifndef uint
//#define uint unsigned int
//#endif

You'll probably need to edit site.cfg and set

threadsafe = False

From MySQL-python-1.2.2/ run:

python setup.py build

sudo python setup.py install

When I tried to run

python setup.py build

I got these errors:

myusername:MySQL-python-1.2.2 myusername$ python setup.py build
sh: mysql_config: command not found
Traceback (most recent call last):
  File "setup.py", line 16, in 
    metadata, options = get_config()
  File "/Users/myusername/MySQL-python-1.2.2/setup_posix.py", line 49, in get_config
    libs = mysql_config("libs")
  File "/Users/myusername/MySQL-python-1.2.2/setup_posix.py", line 24, in mysql_config
    raise EnvironmentError, "%s not found" % mysql_config.path
EnvironmentError: mysql_config not found

I found the answer here (thank you Google!):

http://www.keningle.com/?feed=rss2&p=11

If you met the following error when try to build

mysql_config
    raise EnvironmentError, "%s not found" % mysql_config.path
EnvironmentError: mysql_config not found

Then edit the setup_posix.py file and change the mysql_config.path

mysql_config.path = "/usr/local/mysql/bin/mysql_config"

In my case, I edited the setup_posix.py thusly:

# mysql_config.path = "mysql_config"
mysql_config.path = "/usr/local/mysql-5.0.45-osx10.4-i686/bin/mysql_config"

After the install, don't forget to create the symlink:

sudo ln -s /usr/local/mysql/lib/ /usr/local/mysql/lib/mysql

Verify the installation

>>> import MySQLdb
>>> MySQLdb.apilevel
'2.0'

>>> import django
>>> print django.VERSION
(0, 97, 'pre')

>>> exit()

Verify that django-admin.py is in your path:

django-admin.py

Type 'django-admin.py help' for usage.
Comment by Seamus on Jun 24, 2008:
Thank you. I had found everything but the last step (mysql_config.path) on the web to get it to work. I used: mysql_config.path = "/usr/local/mysql/bin/mysql_config" instead because my MySQL folder is named different, but the mysql is link to the real folder.
Comment by bitbutter on Oct 09, 2008:
Thanks very much for this, i had the same mysqyl_config problem but following this post i was able to fix it. In my case the mysql_config path was: /opt/local/lib/mysql5/bin/mysql_config Before i could get the build to work i also found that i needed to install MacOSX10.4.Universal.pkg from my installation CD, from the XCode packages/tools section.
Comment by Danny on Dec 08, 2008:
I'm having the same problem. But I'm in Ubuntu and I can't figure out the path for the mysql_config.
Comment by elimisteve on Apr 19, 2009:
sudo apt-get install libmysql++-dev to fix *buntu's inability to find mysql_config
Comment by tvc15 on Jun 03, 2009:
gr8 but u have to go thru build/install after you reconfig the poxis.py file ... also cldnt figure out the point of the aliasing?
Comment by jon on Jun 10, 2009:
I have the same problem: mysql_config raise EnvironmentError, "%s not found" % mysql_config.path EnvironmentError: mysql_config not found but the only stuff in my /usr/local folder are these three directories: loads/MySQL-python-1.2.2>ls -la /usr/local . .. bin include lib there's nothing else, as this line of your solution suggests: mysql_config.path = "/usr/local/mysql/bin/mysql_config" the blog you found the answer on has a reply (a reply to a MAMP user, like myself) "For you to have the required libs inside mysql dir you have to compile MySQL with -enable-thread-safe-client option. " I have no idea how to "compile MySQL" (I thought that kind of stuff is what tools like MAMP were for). Bottom line, I don't care what I have to do, I just don't know where to look next :( thanks for any suggestions
Comment by teo on Sep 25, 2009:
I followed your article step by step and installed everything successfully but when i type django-admin.py i get an error--> django-admin.py: command not found. Any help?
Comment by igor on Oct 21, 2009:
Great help. The only problems I had were caused by not following the exact procedure that you listed.
Comment by ruwan on Dec 23, 2009:
thanks, it still works! 'mysql_config' is in mysql-5.1.41-osx10.5-x86 osx 10.5.8
Comment by vukini on Feb 23, 2010:
first I used "locate mysql_config" which gives me the the correct path in snow leopard. Installation went without hiccup. however on entering python I get the following error: $ python Python 2.6.1 (r261:67515, Jul 7 2009, 23:51:51) >>> import MySQLdb Traceback (most recent call last): File "<stdin>", line 1, in <module> File "MySQLdb/__init__.py", line 19, in <module> import _mysql ImportError: No module named _mysql Any ideas?
Comment by vukini on Feb 23, 2010:
Ok the solution to my earlier post below is clear. You must install the MySQLdb in you home folder. I tried installing in Applications and it failed as below. Install in your home folder and all is cool. vukini - 2010-02-23 03:35:04 first I used "locate mysql_config" which gives me the the correct path in snow leopard. Installation went without hiccup. however on entering python I get the following error: $ python Python 2.6.1 (r261:67515, Jul 7 2009, 23:51:51) >>> import MySQLdb Traceback (most recent call last): File "<stdin>", line 1, in <module> File "MySQLdb/__init__.py", line 19, in <module> import _mysql ImportError: No module named _mysql Any ideas?
Comment by Paycor on Feb 03, 2011:
It only took you 15 min.?! Impressive. It took me about a half an hour to get Django/MySQL installed and running on my MAC. That might have less to do with what I was installing, and more to do with the fact that I was installing it on a MAC. I'm a bit of a Macintosh Noob, if you will. Thanks for the tips though. Who knows how long it would have taken without them.
Comment by sarah on May 26, 2011:
Thanks for the django help!
Comment by Rolladen on May 26, 2011:
It works! Thank You!
Comment by Craig on May 29, 2011:
The path to mysql_config for MAMP is /Applications/MAMP/Library/bin/mysql_config