Django启动报错Did you install mysqlclient or MySQL-python

Posted deaconone

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Django启动报错Did you install mysqlclient or MySQL-python相关的知识,希望对你有一定的参考价值。

前言

今天在对数据迁移的时候遇到一坑,这个坑之前也是遇到过,在Google之后解决了,今天又遇到了,不知道怎么处理了,又开始搜索,最终解决。

故此记录问题解决的过程

问题描述

Django项目,迁移数据,执行命令:

python manage.py makemigrations

报错摘录信息:

...

  File "/Users/donghp/.virtualenvs/blog/lib/python3.7/site-packages/django/db/backends/mysql/base.py", line 30, in <module>
    ‘Did you install mysqlclient or MySQL-python?‘ % e
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: dlopen(/Users/donghp/.virtualenvs/blog/lib/python3.7/site-packages/MySQLdb/_mysql.cpython-37m-darwin.so, 2): Library not loaded: @rpath/libmysqlclient.21.dylib
  Referenced from: /Users/donghp/.virtualenvs/blog/lib/python3.7/site-packages/MySQLdb/_mysql.cpython-37m-darwin.so
  Reason: image not found.
Did you install mysqlclient or MySQL-python?

分析

提示说缺少 mysqlclientMySQL-python,可是我 pip list,查看了一下,是有的呀。网上说需要在项目加上一段代码将pymysql 指向 Mysqldb

解答

在项目的根目录__init__.py添加如下代码:

import pymysql

pymysql.install_as_MySQLdb()

注意:

不是manage.py所在的目录

不是manage.py所在的目录

不是manage.py所在的目录

是项目settings所在的目录

├── __init__.py
├── settings.py
├── urls.py
└── wsgi.py

Enjoy your code, good luck.

以上是关于Django启动报错Did you install mysqlclient or MySQL-python的主要内容,如果未能解决你的问题,请参考以下文章

Did you install mysqlclient? 运行Django项目报错

django报错:django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module. Did you install

Django报错“Did you install mysqlclient or MySQL-python?”的解决办法(前提:mysqlclient&MySQL都已经安装)

Django1.8无法连接mysql的问题Did you install mysqlclient or MySQL-python

Error loading MySQLdb module. Did you install mysq

解决报错:from err django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module. Did you ins