Python import MySQLdb error - Mac 10.6


I downloaded and followed the install instructions for MySQL 5.5.8 ( and for the MySQLdb python plugin. (

When I attempt to import MySQLdb to a python terminal, I get the below error:

Safira:~ yanigisawa$ python --version
Python 2.6.1
Safira:~ yanigisawa$ python -c "import MySQLdb"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "build/bdist.macosx-10.6-universal/egg/MySQLdb/", line 19, in <module>
  File "build/bdist.macosx-10.6-universal/egg/", line 7, in <module>
  File "build/bdist.macosx-10.6-universal/egg/", line 6, in __bootstrap__
ImportError: dlopen(/Users/yanigisawa/.python-eggs/MySQL_python-1.2.3-py2.6-macosx-10.6-universal.egg-tmp/, 2): Library not loaded: libmysqlclient.16.dylib
  Referenced from: /Users/yanigisawa/.python-eggs/MySQL_python-1.2.3-py2.6-macosx-10.6-universal.egg-tmp/
  Reason: image not found
Safira:~ yanigisawa$

I found question 4559699 on stack overflow, and followed the instructions there:

Safira:~ yanigisawa$ echo $DYLD_LIBRARY_PATH
Safira:~ yanigisawa$ ls -l /usr/local/mysql/lib
total 363224
-rwxr-xr-x   1 root  wheel   3787328 Jan 18 19:41 libmysqlclient.16.dylib
-rw-r--r--   1 root  wheel  10035336 Dec  3 13:26 libmysqlclient.a
lrwxr-xr-x   1 root  wheel        23 Jan 14 22:01 libmysqlclient.dylib -> libmysqlclient.16.dylib
lrwxr-xr-x   1 root  wheel        20 Jan 14 22:01 libmysqlclient_r.16.dylib -> libmysqlclient.dylib
lrwxr-xr-x   1 root  wheel        16 Jan 14 22:01 libmysqlclient_r.a -> libmysqlclient.a
lrwxr-xr-x   1 root  wheel        20 Jan 14 22:01 libmysqlclient_r.dylib -> libmysqlclient.dylib
-rw-r--r--   1 root  wheel  88033760 Dec  3 13:22 libmysqld-debug.a
-rw-r--r--   1 root  wheel  84075304 Dec  3 13:26 libmysqld.a
-rw-r--r--   1 root  wheel      8472 Dec  3 13:26 libmysqlservices.a
drwxr-xr-x  13 root  wheel       442 Dec  3 13:53 plugin
Safira:~ yanigisawa$ md5 /usr/local/mysql/lib/libmysqlclient.16.dylib
MD5 (/usr/local/mysql/lib/libmysqlclient.16.dylib) = b8a78a93183720021b9dbe9ddad57111
Safira:~ yanigisawa$

And after cleaning all of the python egg files, and rebuilding, I still got the same error. Then I found bug 59006 ( in MySQL 5.5.8 affecting Mac 10.6. I tried the work-around listed there, cleaned eggs and rebuilt, and I'm still getting this error.

Is there another work-around for this problem specific to python? Am I missing something else?

EDIT: Adding output from otool commands:

Safira:~ yanigisawa$ otool -L /Users/yanigisawa/.python-eggs/MySQL_python-1.2.3-py2.6-macosx-10.6-universal.egg-tmp/
    libmysqlclient.16.dylib (compatibility version 16.0.0, current version 16.0.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 125.2.1)
Safira:~ yanigisawa$ otool -DX /usr/local/mysql/lib/libmysqlclient.16.dylib
Safira:~ yanigisawa$

You could try running otool to find out exactly what library paths the MySQLdb C extension, is looking for:

$ otool -L /Users/yanigisawa/.python-eggs/MySQL_python-1.2.3-py2.6-macosx-10.6-universal.egg-tmp/

and then the installed library name of the MySQL library file:

$ otool -DX /usr/local/mysql/lib/libmysqlclient.16.dylib

But you should not have to resort to setting DYLD_LIBRARY_PATH (or using install_name_tool to modify an executable); the need to set it is almost always a sign of a component that was built or installed incorrectly. In my experience, the MySQL project does not have a very good track record for consistency in their OS X builds and installers, as that issue you cite tends to confirm.

Trying to get a working combination of Python, MySQLdb, and MySQL libraries in OS X 10.6 is a very common cause of frustration: lots of questions about it here on SO and elsewhere. There are a number reasons for that. My advice is to go with a complete solution from one of the third-party distributors that will build and install compatible versions of everything you need. And it should keep working if you need to upgrade components as time goes on. I've had good success over the years with MacPorts; others prefer Fink or the newer HomeBrew. With MacPorts, after installing the base files from the MacPorts .dmg, you can get everything built like so:

$ sudo port selfupdate  # ensure the port files are up-to-date
$ sudo port install py26-mysql

The MacPorts Python will be available at /opt/local/bin/python2.6. If you are looking to run Django, there's a port for that as well.

EDIT: With the updated output from otool, you can see that there is a mismatch of library path names. The MySQLdb extension is asking for a relative path name (no initial /), while the MySQL library advertises itself with an absolute path, normally what you want. It seems to be the reverse of the MySQL issue 59006 but, without knowing exactly what steps you've performed up until now, it is really hard to say how you got to this state. You may be able to use install_name_tool to patch the to have the absolute path name which would also eliminate any need to set DYLD_LIBRARY_PATH. It's worth trying:

$ cd ~/.python-eggs/MySQL_python-1.2.3-py2.6-macosx-10.6-universal.egg-tmp
$ sudo install_name_tool -change libmysqlclient.16.dylib \
                                 /usr/local/mysql/lib/libmysqlclient.16.dylib \

But that should not leave you with a warm and fuzzy feeling that you can reproduce the results when you need to update something. There really is no virtue in trying to glue all these pieces from different sources into something that works.