Django和mysql的一些问题django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.3 or newer is required; you have 0.9.3
报错环境
Python==3.7.3 , django==2.2.4, pymysql==0.9.3
报错说明
django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.
解决方法
Django连接MySQL时默认使用MySQLdb驱动,但MySQLdb不支持Python3,因此这里将MySQL驱动设置为pymysql,使用 pip install pymysql 进行安装,然后在工程文件__init__.py添加以下代码即可。
#安装pymysql
pip install pymysql
#__init__.py
import pymysql
pymysql.install_as_MySQLdb()
第一种
Django降版本到2.1.4就OK了
第二种(仍然使用Django2.2版本)
#找到Python环境下 django包,并进入到backends下的mysql文件夹
cd ../site-packages/django/db/backends/mysql
# 找到base.py文件,注释掉 base.py 中如下部分(35/36行)
if version < (1, 3, 3):
raise ImproperlyConfigured("mysqlclient 1.3.3 or newer is required; you have %s" % Database.__version__)
此时仍可能会报错,报错信息如下:
AttributeError: ‘str’ object has no attribute ‘decode’
解决方法
#同样找到Python环境下 django包,并找到operations.py文件
cd ..\site-packages\django\db\backends\mysql\operations.py
#第146行,将decode改为encode
if query is not None:
query = query.decode(errors='replace')
return query
#改为
if query is not None:
query = query.encode(errors='replace')
return query
OK,问题解决。
转载来源:https://blog.youkuaiyun.com/weixin_33127753/article/details/89100552