18

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?

Community
  • 1
  • 1
Dave Everitt
  • 17,193
  • 6
  • 67
  • 97
  • 3
    Followed same confused path - important difference is I think that the latest brew install gives libmysqlclient.20.dylib not 18 and reinstalling MySQL suggested by Aplusplus answered it for me too. – PhoebeB Jan 06 '16 at 11:30
  • for me `pip install mysqlclient` solved the problem – ezdazuzena Feb 27 '17 at 09:36

4 Answers4

32

I also encountered this problem. I uninstalled the MySQL-python, and then installed it.

pip uninstall MySQL-python
pip install MySQL-python

Update (based on comments)

In some cases, you may need to perform the second (install) step in the following manner:

pip install --no-binary MySQL-python MySQL-python

The no-binary option is so that pip builds it fresh and links to the correct library:

--no-binary <format_control>

Do not use binary packages. Can be supplied multiple times, and each time adds to the existing value. Accepts either :all: to disable all binary packages, :none: to empty the set, or one or more package names with commas between them. Note that some packages are tricky to compile and may fail to install when this option is used on them.

NB: Note, that MySQL-python needs to be mentioned twice. As mentioned above, the first occurrence is the name of the package to apply the no-binary option to, the second specifies the package to install.

Dan Mašek
  • 17,852
  • 6
  • 57
  • 85
Shengwei
  • 516
  • 5
  • 7
  • 16
    thanks, that helped but I also had to use `pip install --no-binary MySQL-python MySQL-python` to avoid using a cached wheel – Yeray Diaz Jan 26 '16 at 16:37
  • 2
    Only python 2. Python 3 is not yet supported. :/ – Piotr Migdal Feb 13 '16 at 12:09
  • 3
    I have tried uninstalling and re-installing mysql-python and am still having the same problem. Any other thoughts on how to debug/fix the problem? – Vishal Feb 15 '16 at 23:12
  • I was having this same error in Ruby + Rails after updating my local OS. I uninstalled the gem (`gem uninstall mysql2`) and in my case reinstalled using bundler (`bundle install`), but you could just `gem install mysql2`. – equivalentideas Oct 02 '16 at 07:20
  • With typical pip install didn't work for me, but with --no-binary flag, it works! :D – Jorge Maroto Jan 12 '17 at 09:45
  • It dit not worked for me until I used `pip install --no-cache-dir MYSQL-python`. – hectorcanto Mar 07 '17 at 11:25
  • WOW okay tried for so long and @YerayDiazDiaz's answer finally worked for me! Thanks so much @YerayDiazDiaz!!! We should update the answer to show that. – N V Sep 27 '17 at 23:27
  • mysqlclient has taken over from MySQL-python - `pip install mysqlclient` did the job for me on the new install of a project that formerly used MySQL-python (which hasn't been updated for years). – Dave Everitt Apr 30 '18 at 13:57
16

You need to use dev version of mysqlclient:

pip install git+https://github.com/PyMySQL/mysqlclient-python.git@master

Before I had the lastest PyPI version (1.3.7) on Python 3.4 and it was searching for libmysqlclient.18.dylib (from MySQL 5.6) whereas I had only libmysqlclient.20.dylib(from MySQL 5.7).

If you use Python 3, MySQL-python is not an option (and mysqlclient is its newer version).

Piotr Migdal
  • 11,864
  • 9
  • 64
  • 86
  • 1
    Definitely helped me with my setup: Mac OS X, brew installed python and mysql. – JayNCoke Dec 06 '16 at 20:44
  • This should be the accepted answer! Big question is, why after **more than 18 months is the develop version not yet released?!?** – mkoistinen Nov 24 '17 at 22:00
9

If encountered a problem with lacking of libmysqlclient.18.dylib:

  1. download mysql 5.6 from official link: https://dev.mysql.com/downloads/mysql/

  2. install it

  3. in terminal - mdfind libmysqlclient | grep .18.

  4. copy the output

  5. sudo ln -s [the output from previous command] /usr/local/lib/libmysqlclient.18.dylib

Pardeep Dhingra
  • 3,916
  • 7
  • 30
  • 56
Dar Ben-Tov
  • 111
  • 7
8

This solved my issue on my case:

$ pip uninstall MySQL-python
$ pip install mysqlclient

MySQL-python turned out to be very old (last commit was 7 years ago). mysqlclient is the modern version of it, with lots of improvements and bug fixes.

Ranel Padon
  • 525
  • 6
  • 13
  • 1
    Yep. That's the issue! All over the internet there's "MySQL-python" as the way to go, but the commits tell the real story. – Dave Everitt Apr 30 '18 at 13:55
  • 1
    Mojave on OSX is/was problematic. Use of `brew install mysql-connector-c` installed libmysqlclient.18.dylib where it was expected. Various "updated" software had `libmysqlclient.21.dylib` (and others) via brew and native installs. I opted to get the "18" version on system as MySQL on OSX breaks easily (10 years running) so opt for understandable, reversible, and logical fixes. It is important to mention that Python+MySQL is turbulent on OSX, as is Python+OSX historically. Not even (PIPENV like) virtual env will protect the user/app from volatility, local drivers/assets can fail too. – Marc Dec 26 '18 at 17:51