python 踩坑之解决django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.Did you insta
1. 电脑无意点了升级之后就各种不正常,之前运行好好的django项目,突然出现
我明明是安装了mysqlclient
的,执行pip3 freeze
能看到实际上我已经安装了mysqlclient
。
2:网上的解决办法让我欲哭无泪
第一步:项目(settings.py同级)目录中__init__.py中添加
import pymysql
pymysql.install_as_MySQLdb()
然后出现版本检查报错: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.
第二步:解决版本检查报错
找到Python安装路劲下的/Users/MAC/env/MxShop/lib/python3.6/site-packages/django/db/backends/mysql/base.py文件
将一下代码注释掉
#if version < (1, 3, 3):
# raise ImproperlyConfigured("mysqlclient 1.3.3 or newer is required; you have %s" % Database.__version__)
看似非常完美,本地运行也确实正常了,但是非常蛋疼的事,代码提交到git一部署,所有的问题都来了,我咋我还能跑到阿里云的pod上去修改以上文件源码吗??????
3:正确的解决方式(画重点)
第一步:安装mysql-connector-c
brew install mysql-connector-c
这一步可能会出现红色报错, 没关系,按照绿色步骤安装完以后重新运行brew install mysql-connector-c
第二步:建立链接
brew link mysql-connector-c
在建立链接的过程中会有提示,根据提示内容在~/.zshrc
文件中添加环境变量:
echo 'export PATH="/usr/local/opt/mysql-client/bin:$PATH"' >> ~/.zshrc
有的电脑会提示:
echo 'export PATH="/usr/local/opt/mysql-client/bin:$PATH"' >> ~/.bash_profile
没事提示啥就复制啥,粘贴运行就可以
完事就重新运行一下:
source ~/.zshrc文件,或者source ~/.bash_profile
第三步:删除原本安装的mysqlclient
pip3 uninstall mysqlclient==1.4.4
第四步:不使用缓存地重新安装mysqlclient
pip3 --no-cache-dir install mysqlclient==1.4.4
完美解决
来源:刘宁的博客