Installing Python MySQLdb on OS X Lion

Tonight I tried installing a Python MySQLdb module, but when I ran the setup I got the following error:

sh: mysql_config: command not found
Traceback (most recent call last):
  File "setup.py", line 15, in 
    metadata, options = get_config()
  File "/Users/markotomic/MySQL-python-1.2.3/setup_posix.py", line 43, in get_config
    libs = mysql_config("libs_r")
  File "/Users/markotomic/MySQL-python-1.2.3/setup_posix.py", line 24, in mysql_config
    raise EnvironmentError("%s not found" % (mysql_config.path,))

Looking at the error it was obvious that mysql_config wasn’t in the environment path. To fix this, I added mysql/bin in my bash profile:

PATH="${PATH}:/usr/local/mysql/bin"

Then I tried the install using the following steps:
Download MySQLdb module here

gunzip MySQL-python-1.2.3.tar.gz
$ tar -xvf MySQL-python-1.2.3.tar
$ cd MySQL-python-1.2.3
$ python setup.py build
$ python setup.py install

This went smoothly, however when I tried to test the installation:

>>> import MySQLdb

I got the following error:

/Library/Python/2.7/site-packages/MySQL_python-1.2.3-py2.7-macosx-10.7-intel.egg/_mysql.py:3: UserWarning: Module _mysql was already imported from /Library/Python/2.7/site-packages/MySQL_python-1.2.3-py2.7-macosx-10.7-intel.egg/_mysql.pyc, but /Users/markotomic/MySQL-python-1.2.3 is being added to sys.path
Traceback (most recent call last):
  File "", line 1, in 
  File "MySQLdb/__init__.py", line 19, in 
    import _mysql
  File "build/bdist.macosx-10.7-intel/egg/_mysql.py", line 7, in 
  File "build/bdist.macosx-10.7-intel/egg/_mysql.py", line 6, in __bootstrap__
ImportError: dlopen(/Users/markotomic/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.7-intel.egg-tmp/_mysql.so, 2): no suitable image found.  Did find:
	/Users/markotomic/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.7-intel.egg-tmp/_mysql.so: mach-o, but wrong architecture

The compiled image wasn’t suitable for my 64-bit processor, so I went on and compiled a new one from scratch after reading this post, but still had no luck.

I even tried installing a new version of python with Homebrew, hoping it would set up all my dependencies right and things would just work. Nope.
At this stage I had multiple versions of Python running, so I decided to nuke everything. I removed:

- /Library/Python
- /System/Library/Frameworks/Python.framework
- /usr/bin/python*
-/usr/local/share/python
- sudo brew uninstall python

I’ve re-installed python using the latest OS X binary dmg from the python official website.

Then installed python setuptools

Most importantly, and this is where I probably went wrong from the beginning, downloaded a 64-bit MySQL community server for OS X

After that, I simply ran:

easy_install MySQL-python

And

>>> import MySQLdb

Produced no errors.

If I had the 64-bit MySQL server installed on my system to begin with, I probably wouldn’t have gone through all the steps above. So the lesson learnt from this was to ensure you’re running a 64-bit version of MySQL server before you even try installing MySQLdb module on OS X Lion.

Marko