Quick Tip: Installing MySQLdb for Python on Mac OSX
This quick tip is intended to serve as a quick reference for setting up MySQLdb package for Python on Mac OSX (but this will probably apply on Linux too).
Firstly, download the latest MySQLdb package from sourceforge. Then unzip the file anywhere on the file system. On the command prompt, activate your virtualenv and then change to the unpacked directory;
(myvirtualenv) kevin$ cd ~/Downloads/MySQL-python-1.2.3
Now install it using the setup.py script
(myvirtualenv) kevin$ python setup.py install
If everything went well, it will install correctly and you can confirm by entering a Python shell and trying to import the package.
(myvirtualenv) kevin$ python Python 2.7.2 (default, Jan 31 2012, 18:01:16) [GCC 4.2.1 Compatible Apple Clang 3.0 (tags/Apple/clang-211.10.1)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import MySQLdb >>>
If no errors occur then you’re good to go. If however you get the error;
dlopen(/Users/kevin/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.7-intel.egg-tmp/_mysql.so, 2): Library not loaded: libmysqlclient.18.dylib Referenced from: /Users/kevin/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.7-intel.egg-tmp/_mysql.so Reason: image not found
Then you will need to locate the libmysqlclient.18.dylib file and set DYLD_LIBRARY_PATH environment variable. For me, the library was located here -> /usr/local/mysql-5.5.15-osx10.6-x86_64/lib/libmysqlclient.18.dylib. You can set this in a few places such as your .bash_profile or in the bin/activate file of virtualenv.
export DYLD_LIBRARY_PATH=/usr/local/mysql-5.5.15-osx10.6-x86_64/lib/