django.core.exceptions.ImproperlyConfigured:加载 MySQLdb 模块时出错:没有名为 'MySQLdb' 的模块

django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named 'MySQLdb'

我创建了一个使用 MySQL 数据库的 Django 项目。我在 mysql 安装的工具中有 mysql-python 连接器。我不确定我是否在环境变量中设置了必需的路径。当我 运行 服务器时,它引发错误: django.core.exceptions.ImproperlyConfigured:加载 MySQLdb 模块时出错:没有名为 'MySQLdb' 的模块。如果能帮我解决这个问题,我将不胜感激。

回溯:

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x0000000004F167B8>
Traceback (most recent call last):
  File "c:\python34\lib\site-packages\django\db\backends\mysql\base.py", line 25, in <module>
    import MySQLdb as Database
ImportError: No module named 'MySQLdb'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\python34\lib\site-packages\django\utils\autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "c:\python34\lib\site-packages\django\core\management\commands\runserver.py", line 113, in inner_run
autoreload.raise_last_exception()
  File "c:\python34\lib\site-packages\django\utils\autoreload.py", line 249, in raise_last_exception
    six.reraise(*_exception)
  File "c:\python34\lib\site-packages\django\utils\six.py", line 685, in    reraise
   raise value.with_traceback(tb)
 File "c:\python34\lib\site-packages\django\utils\autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "c:\python34\lib\site-packages\django\__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "c:\python34\lib\site-packages\django\apps\registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "c:\python34\lib\site-packages\django\apps\config.py", line 199, in    import_models
    self.models_module = import_module(models_module_name)
  File "c:\python34\lib\importlib\__init__.py", line 104, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 2231, in _gcd_import
  File "<frozen importlib._bootstrap>", line 2214, in _find_and_load
  File "<frozen importlib._bootstrap>", line 2203, in     _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 1129, in _exec
  File "<frozen importlib._bootstrap>", line 1448, in exec_module
  File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
  File "c:\python34\lib\site-packages\django\contrib\auth\models.py", line 4, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "c:\python34\lib\site-packages\django\contrib\auth\base_user.py", line 52, in <module>
    class AbstractBaseUser(models.Model):
  File "c:\python34\lib\site-packages\django\db\models\base.py", line 119, in __new__
    new_class.add_to_class('_meta', Options(meta, app_label))
  File "c:\python34\lib\site-packages\django\db\models\base.py", line 316, in add_to_class
    value.contribute_to_class(cls, name)
  File "c:\python34\lib\site-packages\django\db\models\options.py", line 214, in contribute_to_class
   self.db_table = truncate_name(self.db_table,  connection.ops.max_name_length())
  File "c:\python34\lib\site-packages\django\db\__init__.py", line 33, in __getattr__
    return getattr(connections[DEFAULT_DB_ALIAS], item)
  File "c:\python34\lib\site-packages\django\db\utils.py", line 211, in    __getitem__
     backend = load_backend(db['ENGINE'])
   File "c:\python34\lib\site-packages\django\db\utils.py", line 115, in load_backend
    return import_module('%s.base' % backend_name)
   File "c:\python34\lib\importlib\__init__.py", line 104, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
   File "c:\python34\lib\site-packages\django\db\backends\mysql\base.py", line 28, in <module>
     raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
 django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module:     No module named 'MySQLdb'

您需要安装:

pip install mysqlclient

运行 pip install mysql-python 在进行任何迁移之前。