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 "", line 15, in 
    metadata, options = get_config()
  File "/Users/markotomic/MySQL-python-1.2.3/", line 43, in get_config
    libs = mysql_config("libs_r")
  File "/Users/markotomic/MySQL-python-1.2.3/", 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:


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 build
$ python 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/ 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/", line 19, in 
    import _mysql
  File "build/bdist.macosx-10.7-intel/egg/", line 7, in 
  File "build/bdist.macosx-10.7-intel/egg/", line 6, in __bootstrap__
ImportError: dlopen(/Users/markotomic/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.7-intel.egg-tmp/, 2): no suitable image found.  Did find:
	/Users/markotomic/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.7-intel.egg-tmp/ 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*
- 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


>>> 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.


  • Justin

    SO awesome thank you. hours and hours you did it!

  • Pingback: frakturmedia » Blog Archive » Setting up Raspberry Pi’s for scraping

  • acouch

    Thanks for posting this. Saved me a lot of time.

  • ouch

    do not delete Python.framework. DO NOT!

    • Marko Tomic (@mtomic)

      @Ouch – that’s a very good advice. Probably worth noting that installing 64-bit MySQL server is all you need to do. I stuffed my Python framework up in the process and had to clean it up, so that was my last resort. Thankfully, that worked.