【Django】错误汇总

Posted by 西维蜀黍 on 2019-11-05, Last Modified on 2021-09-21

Reason: image not found

Problem

通过 python manage.py runserver 0.0.0.0:80 启动 Django 时,报错:

django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: dlopen(/Working/my_project/venv/lib/python2.7/site-packages/_mysql.so, 2): Library not loaded: /usr/local/opt/mysql@5.7/lib/libmysqlclient.20.dylib
  Referenced from: /Working/my_project/venv/lib/python2.7/site-packages/_mysql.so
  Reason: image not found
Unhandled exception in thread started by <_pydev_bundle.pydev_monkey._NewThreadStartupWithTrace instance at 0x10acf42d8>

Solution

这其实是因为在启动 Django 时,python interpreter 为了连接 MySQL,需要 libmysqlclient.20.dylib 文件,而去 /usr/local/opt/mysql@5.7/lib/libmysqlclient.20.dylib 路径下找时,未找到该文件。

而事实上,本机中其实有 libmysqlclient.20.dylib 文件的,我的这个文件位于 /usr/local/mysql-5.7.28-macos10.14-x86_64/lib/libmysqlclient.20.dylib 下。

# 直接创建软链接,有可能会出现因不存在特定文件夹,导致创建失败的情况
$ sudo ln -s /usr/local/mysql-5.7.28-macos10.14-x86_64/lib/libmysqlclient.20.dylib /usr/local/opt/mysql@5.7/lib/libmysqlclient.20.dylib
ln: /usr/local/opt/mysql@5.7/lib/libmysqlclient.20.dylib: No such file or directory

$ mkdir /usr/local/opt/mysql@5.7
$ cd /usr/local/opt/mysql@5.7
$ mkdir lib
$ sudo ln -s /usr/local/mysql-5.7.28-macos10.14-x86_64/lib/libmysqlclient.20.dylib /usr/local/opt/mysql@5.7/lib/libmysqlclient.20.dylib

再次启动 Django 时,一切正常。

Reference