ModuleNotFoundError: No module named 'MySQLdb'


现象

出现: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)再启动,报错

解决方案

安装mysql的客户端

pip install mysqlclient

再次启动,则是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.