場景:Django 項目中使用了0.9.3舊版本的 mysql 資料庫,在生成 app 應用時報錯。
報錯資訊關鍵部分:
[email protected]_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
has been installed beforelibssl-dev
.pip install mysqlclient
其他方法:參考 stack overflow 上的其他建議。
ps:我特意先使用 google 搜尋該問題,第一條搜尋結果就可以解決該問題,而且還很簡單;然後我去百度同樣搜尋,發現很多方法比較複雜,也沒有軟用,治标不治本,還浪費時間。