mac下Django使用MySQL失败

运行

python manage.py makemigrations

会报错

django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.
Did you install mysqlclient?

之后即使安装mysqlclient,仍会报错

解决:

pip install pymysql

在项目目录下的__init__.py里加上

import pymysql  
pymysql.install_as_MySQLdb()

然后在python的安装环境下python3.6/site-packages/django/db/backends/mysql/base.py 中将if语句注释掉,如下:

version = Database.version_info  
# if version < (1, 3, 13):  
#    raise ImproperlyConfigured('mysqlclient 1.3.13 or newer is required; you have %s.' % Database.__version__)

此时再运行python manage.py makemigrations就可以了。
如果仍然报错

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

可以

#找到operations.py文件,将decode改为encode
if query is not None:
    query = query.decode(errors='replace')
return query
#改为
if query is not None:
    query = query.encode(errors='replace')
return query

你可能感兴趣的:(django,mysql,macos)