ImportError: No module named mysql.base, in django project on Ubuntu 11.04 server

Posted

技术标签:

【中文标题】ImportError: No module named mysql.base, in django project on Ubuntu 11.04 server【英文标题】: 【发布时间】:2011-09-02 04:42:11 【问题描述】:

我按照Django Book 中的步骤进行操作,并进入作者解释 hot wo 设置 django 项目以使用数据库的部分。我选择了mysql

我在settings.py 中的设置是:

DATABASES = 
    'default': 
        'ENGINE': 'mysql',               # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'mydatabase',                  # Or path to database file if using sqlite3.
        'USER': 'myname',                # Not used with sqlite3.
        'PASSWORD': 'mypassword',        # Not used with sqlite3.
        'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
    

尝试启动服务器时,会打印以下消息:

Validating models...

Traceback (most recent call last):
  File "/home/me/workspace/mysite/src/mysite/manage.py", line 14, in <module>
    execute_manager(settings)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 442, in execute_manager
    utility.execute()
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 379, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 191, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 220, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/runserver.py", line 67, in handle
    self.run(*args, **options)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/runserver.py", line 78, in run
    self.inner_run(*args, **options)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/runserver.py", line 88, in inner_run
    self.validate(display_num_errors=True)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 243, in validate
    from django.core.management.validation import get_validation_errors
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/validation.py", line 3, in <module>
    from django.contrib.contenttypes.generic import GenericForeignKey, GenericRelation
  File "/usr/local/lib/python2.7/dist-packages/django/contrib/contenttypes/generic.py", line 7, in <module>
    from django.db import connection
  File "/usr/local/lib/python2.7/dist-packages/django/db/__init__.py", line 27, in <module>
    connection = connections[DEFAULT_DB_ALIAS]
  File "/usr/local/lib/python2.7/dist-packages/django/db/utils.py", line 81, in __getitem__
    backend = load_backend(db['ENGINE'])
  File "/usr/local/lib/python2.7/dist-packages/django/db/utils.py", line 23, in load_backend
    return import_module('.base', backend_name)
  File "/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
ImportError: No module named mysql.base

我搜索了一下,发现如果您使用旧版本的 MySQLd,可能会打印此消息。所以我确保我得到了最新版本。并在交互式 python shell 中测试了导入。没关系。

还有其他建议,为什么这不起作用?

我正在开发一个全新安装的 Ubuntu 11.04 版本(Windows 7 中的 Wubi),如果这很重要的话。 =)

【问题讨论】:

你运行的是什么版本的 Django?来自python-django 包的 1.2.5? 1.3 来自项目网站? 查看django.db.utils.load_backend() 的源代码,我认为您应该使用'django.db.backends.mysql' 而不仅仅是'mysql'。这可能只会给你一个不同的错误消息,但更有帮助。 @Tim Yates:我使用的是'django.db.backends.mysql' 版本。我昨天从后备箱里检查了它,就像书中推荐的那样。将ENGINE 设置为'django.db.backends.mysql' 而不是'mysql' 完成了这项工作。非常感谢@Tim。 【参考方案1】:

正确的database setting 是'django.db.backends.mysql'

【讨论】:

如果该后端设置在manage.py inspectdb 之后产生此错误怎么办; django.core.exceptions.ImproperlyConfigured: 'django.db.backends.mysql' isn't an available database backend.

以上是关于ImportError: No module named mysql.base, in django project on Ubuntu 11.04 server的主要内容,如果未能解决你的问题,请参考以下文章

ImportError : No module named graphics

python27 ImportError: No module named site

Python中ImportError: No module named request

ImportError: No module named win32api

ImportError: No module named zookeeper 问题解决

ImportError: No module named PIL