python3 django连接mysql 数据库

详情参考:

https://blog.csdn.net/weixin_33127753/article/details/89100552

https://imshusheng.com/python/216.html

 

报错环境 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
pip install pymysql

进入工程夹修改
#__init__.py
import pymysql
pymysql.install_as_MySQLdb()

#找到Python环境下 django包,并进入到backends下的mysql文件夹
cd /opt/anaconda3/envs/envAGC_Mini/lib/python3.6/site-packages/django/db/backends/mysql
#文件列表如下


# 找到base.py文件,注释掉 base.py 中如下部分(35/36行)
if version < (1, 3, 3):
  raise ImproperlyConfigured("mysqlclient 1.3.3 or newer is required; you have %s" % Database.__version__)

此时仍会会报错,报错信息如下:

AttributeError: ‘str’ object has no attribute ‘decode’

#找到operations.py文件(46行,版本不同行数不同哈~自个儿find一下),将decode改为encode
#linux vim 查找快捷键:?decode
if query is not None:
query = query.decode(errors='replace')
return query
#改为
if query is not None:
query = query.encode(errors='replace')
return query
OK~ 不再报错
---------------------
作者:Hayley-L
来源:CSDN
原文:https://blog.csdn.net/weixin_33127753/article/details/89100552
版权声明:本文为博主原创文章,转载请附上博文链接!

posted on 2019-07-11 18:58  长岛的雪  阅读(412)  评论(0编辑  收藏  举报