python3 下import MySQLdb报错

错误原代码:

Traceback (most recent call last):

  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/MySQLdb/__init__.py", line 18, in

    from . import _mysql

ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/MySQLdb/_mysql.cpython-38-darwin.so, 2): Library not loaded: @rpath/libmysqlclient.21.dylib

  Referenced from: /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/MySQLdb/_mysql.cpython-38-darwin.so

  Reason: image not found

Mac 下装完MySQL以及 pip install mysqlclient之后,运行代码时碰到以上问题,解决方法,添加环境变量:

export DYLD_LIBRARY_PATH="$DYLD_LIBRARY_PATH:/usr/local/mysql/lib/"

转自:https://www.zhihu.com/question/46943134/answer/103539798

你可能感兴趣的:(python3 下import MySQLdb报错)