macos django 导入使用MySQL错误 django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module. Did you install mysqlclient? Library not loaded: /usr/local/opt/openssl/lib/libssl.1.0.0.dylib

error :django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module. Did you install mysqlclient?

  • 启动django后突然发现这个错误
排查思路:

查看 django mysqlclient 相关源码,

try:
    import MySQLdb as Database
except ImportError as err:
    raise ImproperlyConfigured(
        ‘Error loading MySQLdb module.\n‘
        ‘Did you install mysqlclient?‘
) from err

所以只要你导入失败就会有这个异常,有两种可能,一种是没安装,另一种是导入的包有问题


1. 确定包是否安装 mysqlclient

╰>pip3 list
Package                       Version
----------------------------- ---------
.......
mysqlclient                   1.4.4
.......
2. 手动导入MySQL 是否报错

所以尝试:
Python 3.7.2 (v3.7.2:9a3ffc0492, Dec 24 2018, 02:44:43)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/MySQLdb/__init__.py", line 18, in <module>
    from . import _mysql
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/MySQLdb/_mysql.cpython-37m-darwin.so, 2): Library not loaded: /usr/local/opt/openssl/lib/libssl.1.0.0.dylib
  Referenced from: /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/MySQLdb/_mysql.cpython-37m-darwin.so
  Reason: image not found
 
3. 这个是和ssl 相关的内容报错,

终端运行:
brew switch openssl 1.0.2s
问题解决

参考: https://*.com/questions/59006602/dyld-library-not-loaded-usr-local-opt-openssl-lib-libssl-1-0-0-dylib

macos django 导入使用MySQL错误 django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module. Did you install mysqlclient? Library not loaded: /usr/local/opt/openssl/lib/libssl.1.0.0.dylib

上一篇:C#三层架构第五课之DBUtil层设计


下一篇:CSS文字大小