天天看點

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

報錯環境 python=3.6,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
#檔案清單如下
           
django2.2/mysql ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3
# 找到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檔案(46行,版本不同行數不同哈~自個兒find一下),将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~ 不再報錯