python PyMySQL 和 django2.2 版本兼容问题

错误信息为:django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.

错误原因:

因为Django连接MySQL时默认使用MySQLdb驱动,但MySQLdb不支持Python3,因此这里将MySQL驱动设置为pymysql。由此产生的版本兼容问题。

pymysql安装方法:

1
2
3
4
5
6
#安装pymysql
pip install pymysql

#__init__.py
import pymysql
pymysql.install_as_MySQLdb()

解决办法:

1. django降到2.1.4版本

2. 修复源码

2.1 找到Python环境下 django包,并进入到backends下的mysql文件夹

1
2
3
4
#  使用此命令可以看到对应的文件夹目录
➜ daliyfresh pip install pymysql
Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
Requirement already satisfied: pymysql in /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages (0.9.3)

2.2 找到base.py文件,注释掉 base.py 中如下部分(35/36行)

1
2
if version < (1, 3, 3):
raise ImproperlyConfigured("mysqlclient 1.3.3 or newer is required; you have %s" % Database.__version__)

2.3 此时仍然报错,找到operations.py文件,将decode改为encode

1
AttributeError: ‘str’ object has no attribute ‘decode’

解决办法:

1
2
3
4
5
6
7
8
#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

测试,执行数据迁移

1
2
python manage.py makemigrations
python manage.py migrate

python PyMySQL 和 django2.2 版本兼容问题

「点点赞赏,手留余香」

    还没有人赞赏,快来当第一个赞赏的人吧!
博客新報
0 条回复 A 作者 M 管理员
    所有的伟大,都源于一个勇敢的开始!
欢迎您,新朋友,感谢参与互动!欢迎您 {{author}},您在本站有{{commentsCount}}条评论