Python flask Reason: image not found libmysqlclient.21.dylib

Python flask Reason: image not found libmysqlclient.21.dylib

折腾了半个下午,在这里找到了答案,在此记录一下,以免后人躺坑

错误提示:

ImportError: dlopen(/Users/XXX/Library/Python/2.7/lib/python/site-packages/_mysql.so, 2): Library not loaded: /usr/local/opt/mysql/lib/libmysqlclient.21.dylib
  Referenced from: /Users/XXX/Library/Python/2.7/lib/python/site-packages/_mysql.so

Reason: image not found

stackoverflow上解释道:

Are you actually using the long-dead connector MySQL-python, aka MySQLdb? If so, that only works with MySQL 5.x (and was only ever fully tested with 5.0).

原来是我的flask用的mysqldb驱动已经太老,已经不能支持最新的Mysql 8,所以这里将数据库驱动换成更新的pymysql(当然如果没有安装要安装一下pip install pymysql

修改config.py

DIALECT = 'mysql'
DRIVER = 'pymysql'  # 这里改成pymysql驱动
USERNAME = 'root'
PASSWORD = 'root'
HOST = '127.0.0.1'
PORT = '3306'
DATABASE = 'xx'

SQLALCHEMY_DATABASE_URI = "{}+{}://{}:{}@{}:{}/{}?charset=utf8".format(DIALECT, DRIVER, USERNAME, PASSWORD, HOST, PORT, DATABASE)
SQLALCHEMY_TRACK_MODIFICATIONS = False

欸,以后不要稀里糊涂地复制代码了

posted @ 2019-01-26 17:21  Santiego  阅读(956)  评论(0编辑  收藏  举报