場景:Django 項目中使用了0.9.3舊版本的 mysql 數據庫,在生成 app 應用時報錯。
報錯信息關鍵部分:
manage.py@api_testing > startapp login
......
File "D:\env_testing\lib\site-packages\django\db\backends\mysql\base.py", line 36, in <module>
raise ImproperlyConfigured('mysqlclient 1.3.13 or newer is required; you have %s.' % Database.__version__)
django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.
比較好的解決辦法(親測可用):找到引入 pymsql 的文件,然後添加版本信息,添加一行代碼。
改動前:
import pymysql
pymysql.install_as_MySQLdb()
改動後:
import pymysql
pymysql.version_info = (1, 3, 13, "final", 0) # 解決mysql版本問題報錯而添加的代碼
pymysql.install_as_MySQLdb()
原因:
- Why do I know you are using pymysql? Because 0.9.3 is just the latest version of pymysql.
- Why use pymysql instead of mysqlclient for the project? Because it is easier to install. pymysql does not depend on system libraries, while mysqlclient relies on a series of system libraries such as libmysqlclient-dev.
- Why is mysqlclient difficult to install and Django still uses it by default? Because mysqlclient is faster and performs better. So if your project has high performance requirements, I suggest you remove the compatible code above and install mysqlclient in your project. If you need help during the installation of mysqlclient, please refer to this link: How to install Python MySQLdb module using pip?, and ensure
libssl-dev
has been installed beforepip install mysqlclient
.
其他方法:參考 stack overflow 上的其他建議。
ps:我特意先使用 google 搜索該問題,第一條搜索結果就可以解決該問題,而且還很簡單;然後我去百度同樣搜索,發現很多方法比較複雜,也沒有軟用,治標不治本,還浪費時間。