MySQL and Python installed with Homebrew
I installed MySQL and Python with Homebrew on OS X 10.10.5 Yosemite. My Python 2.7 is at python -> ../Cellar/python/2.7.9/bin/python
with a symlink to it at /usr/local/bin/python
.
In /usr/local/bin
there is a symlink:
mysql -> ../Cellar/mysql/5.7.9/bin/mysql
The error
In the Python shell:
>>> import MySQLdb
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/site-packages/MySQLdb/__init__.py", line 19, in <module>
import _mysql
ImportError: dlopen(/usr/local/lib/python2.7/site-packages/_mysql.so, 2): Library not loaded: /usr/local/lib/libmysqlclient.18.dylib
Referenced from: /usr/local/lib/python2.7/site-packages/_mysql.so
Reason: image not found
So I tried:
$ sudo unlink /usr/local/lib/libmysqlclient.18.dylib
followed by:
DYLD_LIBRARY_PATH=/usr/local/mysql/lib/:$DYLD_LIBRARY_PATH
and then (desperation over reason):
$ export DYLD_LIBRARY_PATH=/usr/local/Cellar/mysql/5.7.9/lib
But in both cases import MySQLdb
still tried to import libmysqlclient.18.dylib
.
Then I tried:
$ pip install -U MySQL-python
and got:
Requirement already up-to-date: MySQL-python in /usr/local/lib/python2.7/site-packages
Existing answers
Many answers to this problem on SO suggest manually making an explicit symlink to the library with a version number (in my case libmysqlclient.20.dylib
). However, this seems crude and not future-proof, given the existing symlinks:
in /usr/local/lib
there is
libmysqlclient.dylib -> ../Cellar/mysql/5.7.9/lib/libmysqlclient.dylib
and in /usr/local/Cellar/mysql/5.7.9/lib
we find:
libmysqlclient.20.dylib
with a symlink in the same directory to it:
libmysqlclient.dylib -> libmysqlclient.20.dylib
How to make Python forget libmysqlclient.18.dylib
?
So how can I get Python to forget /usr/local/lib/libmysqlclient.18.dylib
and follow the correct symlink in in /usr/local/lib
to libmysqlclient.dylib
, without manually adding yet another symlink?
pip install mysqlclient
solved the problem