天天看点

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

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

声明:感谢原作者解惑https://blog.csdn.net/weixin_33127753/article/details/89100552#comments

环境:python3.7+django2.2+Pymysql=0.93

解决办法:

    步骤:报错意为“需要mysqlclient 1.3.13或更高版本;您有0.9.3版本”,所以下载更高版本的mysqlclient(大于1.3.13版本)

Django连接MySQL时默认使用MySQLdb驱动,但MySQLdb不支持Python3,因此这里将MySQL驱动设置为pymysql。

首先pip安装pymysql:

#通过pip安装pymysql

pip install pymysql

然后在工程的__init__.py文件中写入如下代码:

#__init__.py

import pymysql

pymysql.install_as_MySQLdb()

绿色字体请勿模仿,作者走了个远路!

因为报错需要更高版本的mysqlclient所以我就去下载了mysqlclient1.4.2版本,可以去官网下载最新版本,也可以手动下载轮子。

当然下载轮子页面太大可以ctrl+f去搜索mysqlclient

然后:

#安装最新版本mysqlclient

pip install mysqlclient==1.4.2

以上绿色字体为作者走的弯路,既然用PyMySql代替mysqlclient了为什么还要下载安装mysqlclient呢?作者脑子一瞬间瓦特了。

    方法一:django版本降至2.14版本以下即可。

如果想继续使用django2.2请使用方法二:

    方法二:找到python文件下的django文件>db文件>backends>mysql>base.py

打开后将如下代码注释:

 if version < (1, 3, 13):

     raise ImproperlyConfigured('mysqlclient 1.3.13 or newer is required; you have %s.' % Database.__version__)

注释好了之后重新启动django服务器:

python manage.py runserver

但是会报一个错误,别着急:

报错信息:

AttributeError: 'str' object has no attribute 'decode'

解决办法:

找到python文件下的django文件>db文件>backends>mysql>operations.py

打开后ctrl+f搜索query.decode

将decode改为encode即可。