網上有說用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。否則老是編輯器報錯。