网上有说用pip直接安装MySQL客户端mysql-python来解决的, 笔者的工作环境是mac, 于是试了试, 控制台返回:
bogon:mysite shinchou$ pip install mysql-python Collecting mysql-python Using cached MySQL-python-1.2.5.zip (108 kB) Preparing metadata (setup.py) ... error error: subprocess-exited-with-error × python setup.py egg_info did not run successfully. │ exit code: 1 ╰─> [8 lines of output] Traceback (most recent call last): File "<string>", line 2, in <module> File "<pip-setuptools-caller>", line 34, in <module> File "/private/var/folders/z1/fsvxnm1d3b5ffm5_8lrm8qmm0000gn/T/pip-install-lge5bybl/mysql-python_77605f69aba54d1989f86a4094d69f51/setup.py", line 13, in <module> from setup_posix import get_config File "/private/var/folders/z1/fsvxnm1d3b5ffm5_8lrm8qmm0000gn/T/pip-install-lge5bybl/mysql-python_77605f69aba54d1989f86a4094d69f51/setup_posix.py", line 2, in <module> from ConfigParser import SafeConfigParser ModuleNotFoundError: No module named 'ConfigParser' [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. error: metadata-generation-failed × Encountered error while generating package metadata. ╰─> See above for output. note: This is an issue with the package mentioned above, not pip. hint: See above for details. [notice] A new release of pip available: 22.3.1 -> 23.0.1 [notice] To update, run: python3.10 -m pip install --upgrade pip
经过一番查询, 发现这个包没有兼容python3, 应该找到setup.py这个文件去修改ConfigParser把这个关键字都改为小写: configparser。但笔者没有这个时间磨叽。最好的方案就是用其他的python客户端替换掉。如何做到呢?
使用pymysql并挂载到django
终端输入以下命令
pip install pymysql
安装完之后, 找到django项目下的__init__.py 写入以下代码:
import pymysql pymysql.install_as_MySQLdb()
另外, 笔者用的是PyCharm, 需要在Settings设置里面找到Project:项目名里有个interpreter, 也同时给IDE编辑一个pymysql。否则老是编辑器报错。