最近看django視頻時,由於視頻較老,在配置數據庫時出現以下錯誤:
報錯環境 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.
PyMySQL 是在 Python3.x 版本中用於連接 MySQL 服務器的一個庫,Python2中則使用mysqldb。PyMySQL 目前版本最高是0.9.3,和django所需版本不符。
解決方案一:
使用低版本django如,django2.1.7。
解決方案二:
不使用 PyMySQL,使用mysqlclient。
解決方案三:
注釋掉django/db/backends/mysql/base.py文件的第35、36行:
#if version < (1, 3, 13): # raise ImproperlyConfigured('mysqlclient 1.3.13 or newer is required; you have %s.' % Database.__version__)
再次開啟服務器,報錯如下:
AttributeError: ‘str’ object has no attribute ‘decode’
定位報錯位置:
if query is not None: query = query.encode(errors='replace') return query
將'decode'改為'encode'即可。