現象
出現:ModuleNotFoundError: No module named 'MySQLdb'
(venv) D:\pycharm\Django_01\myapp>python manage.py runserver 0.0.0.0:8800 Watching for file changes with StatReloader Exception in thread django-main-thread: Traceback (most recent call last): File "D:\pycharm\Django_01\venv\lib\site-packages\django\db\backends\mysql\base.py", line 16, in <module> import MySQLdb as Database ModuleNotFoundError: No module named 'MySQLdb' The above exception was the direct cause of the following exception:
背景
更換django項目的默認數據庫為mysql,然后使用命令行(python manage.py runserver 0.0.0.0:8800)再啟動,報錯
解決方案
1、使用命令行安裝mysql的客戶端
pip install mysqlclient
2、安裝whl, whl包的下載路徑
pip install mysqlclient-1.4.6-cp37-cp37m-win_amd64.whl
再次啟動,則是ok
(venv) D:\pycharm\Django_01\myapp>python manage.py runserver 0.0.0.0:8800
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
You have 17 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
March 22, 2022 - 19:01:41
Django version 3.0, using settings 'myapp.settings'
Starting development server at http://0.0.0.0:8800/
Quit the server with CTRL-BREAK.
