python - 配置不正确 : Error loading MySQLdb module: No module named MySQLdb

标签 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',
)

如果您知道会发生什么,我真的很感激为我指明正确的方向。我也可以在这里发布任何更重要的源文件。

最佳答案

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

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

更新 - 实验:

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

- name: MySQLdb
  version: "latest"

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

        import MySQLdb
        print MySQLdb.apilevel

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

enter image description here

恕我直言,这证明 MySQLdb 在 GAE 中可用,并且可以毫无问题地导入。

关于python - 配置不正确 : Error loading MySQLdb module: No module named MySQLdb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33313902/

相关文章:

python - DRF一对多序列化——缺少字段的AttributeError

python - 在 Python 中从字符串创建路径和文件名

python - 添加 Python 应用程序以接受传入的网络连接

php - 需要一个正则表达式来匹配不带前导 0 的十进制数字

mysql - 查询任意相关表时的 JOIN 与 SELECT IN

python - 如何正确覆盖 Django 中的应用程序?

python - 如何知道 django 应用程序的覆盖率?

python - Django allauth 电子邮件确认仅发送 html 代码

python - 如何在 Django 中排队请求?

php - 将错误存储在数据库中的错误函数