django2.2/mysql ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3

报错环境 python=3.6.5,django=2.2,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了

第二种(仍使用django 2.2版本):

#找到Python环境下 django包,并进入到backends下的mysql文件夹
cd /opt/anaconda3/envs/envAGC_Mini/lib/python3.6/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’

#找到operations.py文件(146行),将decode改为encode
#linux vim 查找快捷键:?decode
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~ 不再报错

原文链接在这里

猜你喜欢

转载自www.cnblogs.com/changqing8023/p/10788005.html