五、Djiango初始使用时遇到的坑(No module named 'MySQLdb')

在settings.py同级文件__init__.py中添加以下文件后

import pymysql
pymysql.install_as_MySQLdb()

执行python manage.py,报错“ModuleNotFoundError: No module named ‘MySQLdb‘”

 

底下提示"django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.Did you install mysqlclient?"

 

于是安装mysqlclient:“pip install mysqlclient”,报错:“ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.”

 

以下内容摘自:https://github.com/PyMySQL/mysqlclient-python

You may need to install the Python 3 and MySQL development headers and libraries like so:

  • $ sudo apt-get install python3-dev default-libmysqlclient-dev build-essential # Debian / Ubuntu
  • % sudo yum install python3-devel mysql-devel # Red Hat / CentOS

Then you can install mysqlclient via pip now:

$ pip install mysqlclient

 通过执行

```

  sudo apt install libmysqlclient-dev

  pip install mysqlclient

``

成功安装

 

然后执行"python manage.py migrate"成功

特别说明!!!

当你使用mysqlclient时,就不需要再在settings.py同级文件__init__.py中添加

```

  import pymysql

  pymysql.install_as_MySQLdb()

```

这串代码添加了会报错:"django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3."

因为添加了,

会覆盖mysqlclient,血的教训!!!

 

五、Djiango初始使用时遇到的坑(No module named 'MySQLdb')

上一篇:简单讲讲MsQLl索引


下一篇:mysql自动执行脚本