Python3 报错 Library not loaded: libmysqlclient.20.dylib

在 python3 import 時的錯誤訊息:

Chuns-MacBook-Pro:mysqlclient chunyuyao$ python3
Python 3.7.0 (default, Jul 23 2018, 20:22:55)
[Clang 9.1.0 (clang-902.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
from MySQLdb import _mysql
Traceback (most recent call last):
File "", line 1, in
File "/usr/local/lib/python3.7/site-packages/MySQLdb/init.py", line 18, in
from . import _mysql
ImportError: dlopen(/usr/local/lib/python3.7/site-packages/MySQLdb/_mysql.cpython-37m-darwin.so, 2): Library not loaded: /usr/local/opt/mysql/lib/libmysqlclient.20.dylib
Referenced from: /usr/local/lib/python3.7/site-packages/MySQLdb/_mysql.cpython-37m-darwin.so
Reason: image not found

實際到 /usr/local/opt/mysql/lib/ 去 ls 結果:

-rw-r--r--   1 chunyuyao  staff  5419384 Mar  3 02:17 libmysqlclient.21.dylib
-r--r--r-- 1 chunyuyao staff 6052712 Jan 26 06:50 libmysqlclient.a
lrwxr-xr-x 1 chunyuyao staff 23 Jan 26 06:50 libmysqlclient.dylib -> libmysqlclient.21.dylib
-rw-r--r-- 1 chunyuyao staff 565184 Mar 3 02:17 libmysqlharness.1.dylib
-r--r--r-- 1 chunyuyao staff 1060048 Jan 26 06:50 libmysqlharness.a
lrwxr-xr-x 1 chunyuyao staff 23 Jan 26 06:50 libmysqlharness.dylib -> libmysqlharness.1.dylib
-r--r--r-- 1 chunyuyao staff 6140608 Mar 3 02:17 libmysqlrouter.1.dylib
lrwxr-xr-x 1 chunyuyao staff 22 Jan 26 06:50 libmysqlrouter.dylib -> libmysqlrouter.1.dylib
-r--r--r-- 1 chunyuyao staff 9760 Jan 26 06:50 libmysqlservices.a
drwxr-xr-x 7 chunyuyao staff 224 Jan 26 06:50 mysqlrouter
drwxr-xr-x 3 chunyuyao staff 96 Mar 3 02:17 pkgconfig
drwxr-xr-x 98 chunyuyao staff 3136 Jan 26 06:50 plugin

應該是在 macOS 裡執行 brew upgrade 导致 mysql 有檔案不見,解法:

ln -s libmysqlclient.21.dylib libmysqlclient.20.dylib

brew install mysql 不只讓python3 出錯,python2 也壞掉,我的環境錯誤訊息:

ImportError: dlopen(/Library/Python/2.7/site-packages/_mysql.so, 2): Library not loaded: /usr/local/opt/mariadb/lib/libmysqlclient.18.dylib
Referenced from: /Library/Python/2.7/site-packages/_mysql.so
Reason: image not found

解法,先讓 /usr/local/opt/mysql link to /usr/local/opt/mariadb

再 ln -s libmysqlclient.21.dylib libmysqlclient.18.dylib

搞定。 ^_^


測試的方法,分別使用 python2 和 python3 測試下面這行指令:

import MySQLdb

接下來,是連進 mysql 裡:
Python 連 mysql
http://stackoverflow.max-everyday.com/2017/09/python-mysql/

相關文章

寫留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *