django服务启动失败原因
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module. Did you install mysqlclient?
python3 中django连接mysql使用的包是pymysql, 所以要使用pymysql 代替MySQLdb
1、用requirements.txt来管理所有的依赖包
1)生成requirements.txt文件
在项目路径下执行:
pip freeze > requirements.txt
2)安装requirements.txt依赖
pip install -r requirements.txt
2、在项目的__init__.py中使用pymysql 代替MySQLdb
项目路径如下:
myblog/
├── apps
├── Dockerfile
├── manage.py
├── myblog
│ ├── __init__.py # 此处添加代码
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── statics
└── templates
在__init__.py中添加如下代码,其中pymysql的版本可以指定具体如下,使用的python的版本为3.6,Django==3.1.5
import pymysql
pymysql.version_info = (1, 4, 0, “final”, 0)
pymysql.install_as_MySQLdb()
注意:
大部分博客中推荐的解决上述的报错的方法为:
import pymysql
pymysql.install_as_MySQLdb()
没有指定pymysql的版本,这种解决方法会出现以下的问题:
raise ImproperlyConfigured('mysqlclient 1.4.0 or newer is required; you have %s.' % Database.__version__)
django.core.exceptions.ImproperlyConfigured: mysqlclient 1.4.0 or newer is required; you have 0.10.0.
因此直接指定pymysql的版本可以解决该问题
[1]: https://www.cnblogs.com/qianzf/p/13468170.html