使用mysql-server设置django时出现异常

时间:2014-02-08 16:06:50

标签: python mysql django python-2.7 ubuntu

首先,我在我的Ubuntu机器上安装了django

sudo apt-get install python-django

之后,我使用以下命令创建了我的第一个项目,并创建了一个名为“mysite”的目录。

django-admin.py startproject mysite

在这里,运行以下命令启动服务器并在浏览器中检查,django附带的最小服务器是完美的。

python manage.py runserver

然后,我安装了运行此命令的mysql-server

sudo apt-get install mysql-server

在此之后,我在名为'django_first'的mysql中创建数据库 并使用以下命令成功安装python模块mysqldb。

sudo apt-get install python-mysqldb

截至目前尚未发生任何问题。但是,在设置settings.py文件中的所有必填字段

之后,我立即在目录中运行此命令
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'django_first',
        'USER': 'root',
        'PASSWORD': 'password',
        'HOST': '',
        'PORT': '',
}

现在运行此命令      python manage.py runserver

我收到以下错误: -

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line  399, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 242, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 280, in execute
    translation.activate('en-us')
  File "/usr/local/lib/python2.7/site-packages/django/utils/translation/__init__.py", line 130, in activate
    return _trans.activate(language)
  File "/usr/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 188, in activate
    _active.value = translation(language)
  File "/usr/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py",     line 177, in translation
    default_translation = _fetch(settings.LANGUAGE_CODE)    
  File "/usr/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 159, in _fetch
    app = import_module(appname)
  File "/usr/local/lib/python2.7/site-packages/django/utils/importlib.py", line 40, in     import_module
    __import__(name)
  File "/usr/local/lib/python2.7/site-packages/django/contrib/admin/__init__.py", line 6, in <module>
    from django.contrib.admin.sites import AdminSite, site
  File "/usr/local/lib/python2.7/site-packages/django/contrib/admin/sites.py", line 4,     in <module>
    from django.contrib.admin.forms import AdminAuthenticationForm
  File "/usr/local/lib/python2.7/site-packages/django/contrib/admin/forms.py", line 6, in <module>
    from django.contrib.auth.forms import AuthenticationForm
  File "/usr/local/lib/python2.7/site-packages/django/contrib/auth/forms.py", line 17, in <module>
    from django.contrib.auth.models import User
  File "/usr/local/lib/python2.7/site-packages/django/contrib/auth/models.py", line 48, in <module>
    class Permission(models.Model):
  File "/usr/local/lib/python2.7/site-packages/django/db/models/base.py", line 96, in     __new__
    new_class.add_to_class('_meta', Options(meta, **kwargs))
  File "/usr/local/lib/python2.7/site-packages/django/db/models/base.py", line 264, in     add_to_class
    value.contribute_to_class(cls, name)
  File "/usr/local/lib/python2.7/site-packages/django/db/models/options.py", line 124, in contribute_to_class
    self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
  File "/usr/local/lib/python2.7/site-packages/django/db/__init__.py", line 34, in     __getattr__
    return getattr(connections[DEFAULT_DB_ALIAS], item)
  File "/usr/local/lib/python2.7/site-packages/django/db/utils.py", line 198, in     __getitem__
    backend = load_backend(db['ENGINE'])  
  File "/usr/local/lib/python2.7/site-packages/django/db/utils.py", line 113, in     load_backend
    return import_module('%s.base' % backend_name)
  File "/usr/local/lib/python2.7/site-packages/django/utils/importlib.py", line 40, in     import_module
    __import__(name)
  File "/usr/local/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line     17, in <module>
    raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module     named MySQLdb`

看起来是未安装mysqldb模块的一些问题,但这并不是因为我为此重新运行安装命令。

有关于此的任何想法吗?

3 个答案:

答案 0 :(得分:1)

MySQLdb只是连接到MySQL的模块,但它是名为MySQL-Python的更大包的一部分。只是做:

pip install MySQL-python

它应该照顾一切(你可能需要sudo那个命令)。事实上,我可能建议你这样做:

pip install MySQL-python --upgrade

确保安装最新版本。如果这没有改变任何内容,请按照these instructions确保您获得最新版本和所有必需的依赖项

P.S。通常,总是更喜欢pip而不是apt-get来安装python包,因为它会更频繁地更新。例如,使用django方法安装的apt-get版本可能是一个非常旧的版本。要解决此问题,请删除站点包中的django文件夹并执行:

pip install django==1.6.2 #this is the latest version

答案 1 :(得分:1)

在尝试了上面给出的所有建议后,我设法找出了没有链接到MySQLdb模块的问题。

我决定构建代码并从这里安装源代码。     http://sourceforge.net/projects/mysql-python/

在构建之前,您需要确保以下先决条件:

  
      
  1. gcc编译器
  2.   
  3. MySQL服务器。我使用了MySQL社区服务器版
  4.   

也可以运行这些命令:

sudo apt-get install python-setuptools python-dev libmysqlclient15-dev

如果您收到mysql_config错误,请不要忘记修复PATH env。变量

export PATH=$PATH:/usr/local/mysql/bin

现在我们可以编译代码: -

sudo python setup.py build
sudo python setup.py install

现在打开你的python命令行并导入新的MySQLdb模块

$> python
>>> import MySQLdb

您不应该看到任何导入错误。

答案 2 :(得分:0)

检查是否安装了libmysqlclient-dev? 如果未安装,请尝试安装

  
    

sudo apt-get install libmysqlclient-dev

  

希望这会解决您的问题