ImproperlyConfigured:加载MySQLdb模块时出错:没有名为MySQLdb的模块

时间:2015-10-24 02:25:44

标签: python mysql django google-app-engine module

我正在开发Google App Engine项目,并且在尝试使用MySQL设置基本Django管理站点时遇到了问题。我一直在搜索这个问题,但我看到所有人都发布了关于在本地运行应用程序的交易。在我的情况下,我在Google App Engine中运行应用时遇到错误。

为了更具体地说明问题,我在应用程序的日志中收到以下错误:

ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb

这使得看起来甚至没有安装MySQL,这真的没有意义。它应该在GAE中默认可访问。我还没有看到另一个问题吗?这对我来说很困惑,因为它甚至没有给我一个特定的线条来看待,或者任何类型的线索。

我已经查看了关于Django和CloudSQL的GAE文档,但我没有看到任何不合适的地方。我还看了一下示例Django应用程序,我的所有内容似乎都与他们在那里的内容发生冲突。

完整的日志在这里:

    E 21:59:53.098 Internal Server Error: /admin/
  Traceback (most recent call last):
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.4/django/core/handlers/base.py", line 87, in get_response
      response = middleware_method(request)
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.4/django/contrib/sessions/middleware.py", line 10, in process_request
      engine = import_module(settings.SESSION_ENGINE)
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.4/django/utils/importlib.py", line 35, in import_module
      __import__(name)
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.4/django/contrib/sessions/backends/db.py", line 3, in <module>
      from django.db import IntegrityError, transaction, router
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.4/django/db/__init__.py", line 40, in <module>
      backend = load_backend(connection.settings_dict['ENGINE'])
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.4/django/db/__init__.py", line 34, in __getattr__
      return getattr(connections[DEFAULT_DB_ALIAS], item)
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.4/django/db/utils.py", line 92, in __getitem__
      backend = load_backend(db['ENGINE'])
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.4/django/db/utils.py", line 24, in load_backend
      return import_module('.base', backend_name)
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.4/django/utils/importlib.py", line 35, in import_module
      __import__(name)
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.4/django/db/backends/mysql/base.py", line 16, in <module>
      raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
  ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb
E 21:59:53.227 Traceback (most recent call last):
    File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 267, in Handle
      result = handler(dict(self._environ), self._StartResponse)
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.4/django/core/handlers/wsgi.py", line 241, in __call__
      response = self.get_response(request)
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.4/django/core/handlers/base.py", line 177, in get_response
      response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.4/django/core/handlers/base.py", line 219, in handle_uncaught_exception
      return debug.technical_500_response(request, *exc_info)
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.4/django/views/debug.py", line 66, in technical_500_response
      html = reporter.get_traceback_html()
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.4/django/views/debug.py", line 287, in get_traceback_html
      return t.render(c)
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.4/django/template/base.py", line 140, in render
      return self._render(context)
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.4/django/template/base.py", line 134, in _render
      return self.nodelist.render(context)
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.4/django/template/base.py", line 823, in render
      bit = self.render_node(node, context)
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.4/django/template/debug.py", line 74, in render_node
      return node.render(context)
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.4/django/template/debug.py", line 84, in render
      output = self.filter_expression.resolve(context)
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.4/django/template/base.py", line 599, in resolve
      new_obj = func(obj, *arg_vals)
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.4/django/template/defaultfilters.py", line 718, in date
      return format(value, arg)
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.4/django/utils/dateformat.py", line 310, in format
      return df.format(format_string)
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.4/django/utils/dateformat.py", line 33, in format
      pieces.append(force_unicode(getattr(self, piece)()))
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.4/django/utils/dateformat.py", line 214, in r
      return self.format('D, j M Y H:i:s O')
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.4/django/utils/dateformat.py", line 33, in format
      pieces.append(force_unicode(getattr(self, piece)()))
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.4/django/utils/encoding.py", line 71, in force_unicode
      s = unicode(s)
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.4/django/utils/functional.py", line 121, in __unicode_cast
      return func(*self.__args, **self.__kw)
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.4/django/utils/translation/__init__.py", line 86, in ugettext
      return _trans.ugettext(message)
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.4/django/utils/translation/trans_real.py", line 278, in ugettext
      return do_translate(message, 'ugettext')
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.4/django/utils/translation/trans_real.py", line 268, in do_translate
      _default = translation(settings.LANGUAGE_CODE)
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.4/django/utils/translation/trans_real.py", line 183, in translation
      default_translation = _fetch(settings.LANGUAGE_CODE)
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.4/django/utils/translation/trans_real.py", line 160, in _fetch
      app = import_module(appname)
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.4/django/utils/importlib.py", line 35, in import_module
      __import__(name)
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.4/django/contrib/admin/__init__.py", line 3, in <module>
      from django.contrib.admin.helpers import ACTION_CHECKBOX_NAME
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.4/django/contrib/admin/helpers.py", line 2, in <module>
      from django.contrib.admin.util import (flatten_fieldsets, lookup_field,
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.4/django/contrib/admin/util.py", line 1, in <module>
      from django.db import models
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.4/django/db/__init__.py", line 40, in <module>
      backend = load_backend(connection.settings_dict['ENGINE'])
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.4/django/db/__init__.py", line 34, in __getattr__
      return getattr(connections[DEFAULT_DB_ALIAS], item)
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.4/django/db/utils.py", line 92, in __getitem__
      backend = load_backend(db['ENGINE'])
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.4/django/db/utils.py", line 24, in load_backend
      return import_module('.base', backend_name)
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.4/django/utils/importlib.py", line 35, in import_module
      __import__(name)
    File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/django-1.4/django/db/backends/mysql/base.py", line 16, in <module>
      raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
  ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb

settings.py的相关部分:

import os

DEBUG = True
TEMPLATE_DEBUG = DEBUG

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

DATABASES = {
    'default': {
    'ENGINE': 'django.db.backends.mysql',
    'NAME': 'userinformation',
    'USER': 'root',
    'HOST': '/cloudsql/eloquent-ratio-109701:api-instance',
    }
}

TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
    'django.template.loaders.eggs.Loader',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

ROOT_URLCONF = 'Chimera.urls'

WSGI_APPLICATION = 'Chimera.wsgi.app'

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.admin',
    'django.contrib.admindocs',
    'API',
)

如果您对可能发生的事情有任何了解,我真的很感谢指出我正确的方向。我也可以在这里发布任何更重要的源文件。

1 个答案:

答案 0 :(得分:0)

您是否在MySQLdb配置中指定了app.yaml库?

GAE对使用Django有一些特定的支持,我强烈建议您浏览Django Support文档和Django App example

更新 - 实验:

我刚将此添加到我的(非Django)应用的libraries文件的.yaml部分:

- name: MySQLdb
  version: "latest"

并将其添加到我的一位处理程序中:

        import MySQLdb
        print MySQLdb.apilevel

在GAE上部署更改并触发处理程序后,我获得了此日志条目(与我的开发服务器上的2.0内容相同):

enter image description here

GAHO证明了MySQLdb哪些可用,并且可以毫无问题地导入。