I wanted to make my Django blog engine work on Leopard using MySQL as a database engine. I had a hard time making it work, partially because of my lack of knowledge of Python, partially because I am using MAMP instead of a “/usr/local/mysql”-like MySQL installation, partially because of Leopard itself.
The problem is, basically, that if you try to do the “easy_install MySQL_python” thing, it won’t work in Leopard (the compilation of the native code fails). Here’s how I made it work, following the instructions in this post in the MySQL forums, and doing some tweaking manually.
- Download MySQL_python source code from SourceForge
- Download MySQL’s source code for Mac OS X in tarball form (watch out, there’s code for Universal binaries, and for platform-specific ones – PowerPC or Intel!)
- Unpack both files; this will generate two folders, one mysql-5.0.45-osx10.4-powerpc (I’m on a G4 Mac, your folder might end with “intel”) and MySQL-python-1.2.2
- In the MySQL-python-1.2.2 folder, edit the _mysql.c file; comment out lines 37 to 39:
- If you use MAMP, you should add to your PATH the location of the “include” and “lib” directories (to allow proper compilation and linking of the extension, as you might have imagined); in tcsh you would do something like this:
setenv PATH /Users/adrian/Downloads/mysql-5.0.45-osx10.4-powerpc/include:/Users/adrian/Downloads/mysql-5.0.45-osx10.4-powerpc/lib:$PATH
(of course, replace “adrian” with your own user name ;) - Now build and install MySQL_python as you would normally do:
python setup.py build
sudo python setup.py install - If you have MySQL installed in /usr/local/mysql, then the MySQL_python code should work right now. If it doesn’t (because you use MAMP), you should copy the contents of the mysql-5.0.45-osx10.4-powerpc/lib folder into “/usr/local/mysql/lib/mysql/” (pay attention to the fact that there is “mysql” twice in that path name). This is needed because the code is compiled in such a way that it loads the MySQL library dynamically, and the code is configured to do so.
After this, I could change the configuration of the Django blog code to use the MAMP MySQL database:
Pay attention to the fact that Django is prepared to use the MySQL socket instead of the hostname, when the DATABASE_HOST variable starts with a slash (“/”).
Now, create an empty “blog” database using phpMyAdmin, and a simple
python manage.py syncdb
will create an empty schema for you to use in the application.
Phew! Hope this helps! :)
Similar Posts:

5 Comments
01.15.2008
Jeez man, just install Linux! ;-)
Just type “apt-get install python-mysqldb” and sit back 5 seconds.
Maybe it would be a good idea to create a package system similar to “apt” under Mac. Your next project?
01.15.2008
There are two of them actually ;) http://www.macports.org/ and http://www.finkproject.org/ but… I like to do things by hand :)
01.25.2008
still doesn’t work for me…
the following is the configure statement i used to get the libraries installed for mysql…
mysql is working but even with the steps metioned here i can’t get mysql-python working…
here’s my MySQL configure statement…
MACOSX_DEPLOYMENT_TARGET=10.5 \
CC=gcc \
CFLAGS=”-O3 -fno-omit-frame-pointer \
-arch i386 -arch x86_64 -arch ppc7400 -arch ppc64″ \
CXX=gcc \
CXXFLAGS=”-O3 -fno-omit-frame-pointer -felide-constructors \
-fno-exceptions -fno-rtti \
-arch i386 -arch x86_64 -arch ppc7400 -arch ppc64″ \
./configure –prefix=/usr/local/mysql \
–disable-dependency-tracking –with-extra-charsets=complex –enable-thread-safe-client \
–enable-local-infile –disable-shared
and here’s the error i get with mysql-python…
ImproperlyConfigured: Error loading MySQLdb module: dlopen(/var/tmp/.python_eggs/MySQL_python-1.2.2-py2.5-macosx-10.5-i386.egg-tmp/_mysql.so, 2): no suitable image found. Did find:
/var/tmp/.python_eggs/MySQL_python-1.2.2-py2.5-macosx-10.5-i386.egg-tmp/_mysql.so: no matching architecture in universal wrapper
03.07.2008
It worked for me, thanks a lot!!
04.24.2009
Works fine, except you must take the MySQL binaries not the sources :)
Commenting