Saturday, May 5, 2012

How to install the MySQLdb Python module on OS X Lion

These instructions have been tested on a machine running OS X Lion and Python 2.7.1, but should work for most any Intel setup. Most of the setup is explained in the README file included with the source, and the error at the end was solved per a thread here.
  1. Download MySQL for Python (the MySQLdb module) source from and extract the tar.gz file.
  2. Open Terminal and navigate to the source folder.
  3. Type the following to build the module: python build
  4. Then, install the module by typing the following: sudo python install
  5. In a perfect world, this would be all you'd need to do. However, if your computer was like mine, when you try to import the newly-installed module, it'll through the following error: >>> import MySQLdb 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/username/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.7-intel.egg-tmp/, 2): Library not loaded: libmysqlclient.18.dylib Referenced from: /Users/username/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.7-intel.egg-tmp/ Reason: image not found To fix this error, we'll simply make a symlink where libmysqlclient.18.dylib is expected to be to where it really is: sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib Note: you can also fix this issue by changing your path during install as per this suggestion.


Post a Comment