django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.

  • A+
所属分类:BLOG MySQL Python

今天在linux系统部署程序时报错,django2.2 mysql 8.0 错误信息如下:

python manage.py makemigrations

django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.

首先想到的是,升级mysqlclient版本:

pip install --upgrade mysqlclient
Collecting mysqlclient
  Downloading https://files.pythonhosted.org/packages/f4/f1/3bb6f64ca7a429729413e6556b7ba5976df06019a5245a43d36032f1061e/mysqlclient-1.4.2.post1.tar.gz (85kB)
     |████████████████████████████████| 92kB 47kB/s 
Installing collected packages: mysqlclient
  Running setup.py install for mysqlclient ... done
Successfully installed mysqlclient-1.4.2.post1

完了执行python manage.py makemigrations 还是报错!

通过google找到的解决方案如下:

Django连接MySQL时默认使用MySQLdb驱动,但MySQLdb不支持Python3,因此这里将MySQL驱动设置为pymysql,使用 pip install pymysql 进行安装,然后在工程文件init.py添加以下代码即可。

#安装pymysql
pip install pymysql

#__init__.py
import pymysql
pymysql.install_as_MySQLdb()
  1. 把django版本降到2.1.4即可
  2. 继续使用2.2版本
#找到Python环境下 django包,并进入到backends下的mysql文件夹
cd /home/aaa/.pyenv/versions/getRivalData/lib/python3.6/site-packages/django/db/backends/mysql/
# 找到base.py文件,注释掉 base.py 中如下部分
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文件,将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

最后,问题解决,我们就可以愉快的使用django了

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: