改善
Kaizen  · Today I Learned by Ville Säävuori

Fixing "Error loading MySQLdb module" on macOS 10.15 Catalina

MySql gets usually installed on MacOS by Brew, which installs a bunch of dylib libraries in /usr/local/opt/mysql/lib/. For some reason my Python installation is looking for wrong version of these files and I’ve yet to find a proper solution for matching up the dependencies.

The error message looks like this:

  'Did you install mysqlclient or MySQL-python?' % e
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: dlopen(/Users/uninen/.envs/slipmatio/lib/python2.7/site-packages/MySQLdb/_mysql.so, 2): Library not loaded: /usr/local/opt/mysql/lib/libmysqlclient.20.dylib
  Referenced from: /Users/uninen/.envs/slipmatio/lib/python2.7/site-packages/MySQLdb/_mysql.so
  Reason: image not found.
Did you install mysqlclient or MySQL-python?

This old project uses mysqlclient which is installed properly but it seems that a recent brew update broke things. The library it wants (for MySQL 8) is libmysqlclient.20.dylib yet there is only a libmysqlclient.21.dylib in the lib folder. So, I just manually symlinked that in place and it seems to work fine. Obviously, not a proper solution, but so far only one that gets my local installation working.

> cd /usr/local/opt/mysql/lib/
> ln -s libmysqlclient.21.dylib libmysqlclient.20.dylib
> ls -la

drwxr-xr-x   17 uninen  staff      544 Dec 17 14:49 ./
drwxr-xr-x   19 uninen  staff      608 Dec 17 14:38 ../
lrwxr-xr-x    1 uninen  staff       23 Dec 17 14:49 libmysqlclient.20.dylib@ -> libmysqlclient.21.dylib
-rw-r--r--    1 uninen  staff  7322032 Dec 17 14:38 libmysqlclient.21.dylib
-r--r--r--    1 uninen  staff  8137760 Sep 23 16:05 libmysqlclient.a
lrwxr-xr-x    1 uninen  staff       23 Sep 23 16:05 libmysqlclient.dylib@ -> libmysqlclient.21.dylib

There are a couple of 8+ years old related discussions on StackOverflow, not sure if this is a py27 issue or something else.