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

Posted

技术标签:

【中文标题】django.core.exceptions.ImproperlyConfigured:加载 MySQLdb 模块时出错:没有名为“MySQLdb”的模块【英文标题】:django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named 'MySQLdb' 【发布时间】:2017-04-22 08:51:22 【问题描述】:

我创建了一个使用 mysql 数据库的 django 项目。我在 mysql 安装工具中有 mysql-python 连接器。我不确定我是否在环境变量中设置了必需的路径。当我运行服务器时,它会引发错误: 。我将不胜感激帮助我解决这个问题。

追溯:

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'

【问题讨论】:

【参考方案1】:

你需要安装:

pip install mysqlclient

【讨论】:

当我这样做时,它会引发另一个错误 - 错误:需要 Microsoft Visual C++ 10.0。使用“Microsoft Windows SDK 7.1”获取它:www.microsoft.com/download/details.aspx?id=8279。目前我正在使用aws microsoft local server 2012。我不是用户,它有足够的空间来安装microsoft c++ 10.0。谢谢。请帮我解决这个问题。【参考方案2】:

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

【讨论】:

以上是关于django.core.exceptions.ImproperlyConfigured:加载 MySQLdb 模块时出错:没有名为“MySQLdb”的模块的主要内容,如果未能解决你的问题,请参考以下文章