场景: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 搜索该问题,第一条搜索结果就可以解决该问题,而且还很简单;然后我去百度同样搜索,发现很多方法比较复杂,也没有软用,治标不治本,还浪费时间。