"没有模块名称mysqldb" django中的错误

时间:2015-03-14 13:31:17

标签: python mysql django database

我在ubantu 14.04上用mysql数据库引擎安装了django 1.7(python 2.7.6),但运行命令 -

  

python manage.py migrate

我收到以下错误

(env1)shiv@shiv-Calpella:~/Project/Poll_app/mysite$ python manage.py migrate
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/shiv/Envs/env1/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/home/shiv/Envs/env1/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute
    django.setup()
  File "/home/shiv/Envs/env1/local/lib/python2.7/site-packages/django/__init__.py", line 21, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/shiv/Envs/env1/local/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "/home/shiv/Envs/env1/local/lib/python2.7/site-packages/django/apps/config.py", line 202, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/shiv/Envs/env1/local/lib/python2.7/site-packages/django/contrib/auth/models.py", line 40, in <module>
    class Permission(models.Model):
  File "/home/shiv/Envs/env1/local/lib/python2.7/site-packages/django/db/models/base.py", line 122, in __new__
    new_class.add_to_class('_meta', Options(meta, **kwargs))
  File "/home/shiv/Envs/env1/local/lib/python2.7/site-packages/django/db/models/base.py", line 297, in add_to_class
    value.contribute_to_class(cls, name)
  File "/home/shiv/Envs/env1/local/lib/python2.7/site-packages/django/db/models/options.py", line 166, in contribute_to_class
    self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
  File "/home/shiv/Envs/env1/local/lib/python2.7/site-packages/django/db/__init__.py", line 40, in __getattr__
    return getattr(connections[DEFAULT_DB_ALIAS], item)
  File "/home/shiv/Envs/env1/local/lib/python2.7/site-packages/django/db/utils.py", line 242, in __getitem__
    backend = load_backend(db['ENGINE'])
  File "/home/shiv/Envs/env1/local/lib/python2.7/site-packages/django/db/utils.py", line 108, in load_backend
    return import_module('%s.base' % backend_name)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/shiv/Envs/env1/local/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 18, in <module>
    raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb

我已尝试通过命令安装mysqldb

  

pip install MySQL-python

然后我得到以下错误

    (env1)shiv@shiv-Calpella:~/Project/Poll_app/mysite$ pip install MySQL-pythonCollecting MySQL-python
  Using cached MySQL-python-1.2.5.zip
    sh: 1: mysql_config: not found
    Traceback (most recent call last):
      File "<string>", line 20, in <module>
      File "/tmp/pip-build-3FRYWs/MySQL-python/setup.py", line 17, in <module>
        metadata, options = get_config()
      File "setup_posix.py", line 43, in get_config
        libs = mysql_config("libs_r")
      File "setup_posix.py", line 25, in mysql_config
        raise EnvironmentError("%s not found" % (mysql_config.path,))
    EnvironmentError: mysql_config not found
    Complete output from command python setup.py egg_info:
    sh: 1: mysql_config: not found

    Traceback (most recent call last):

      File "<string>", line 20, in <module>

      File "/tmp/pip-build-3FRYWs/MySQL-python/setup.py", line 17, in <module>

        metadata, options = get_config()

      File "setup_posix.py", line 43, in get_config

        libs = mysql_config("libs_r")

      File "setup_posix.py", line 25, in mysql_config

        raise EnvironmentError("%s not found" % (mysql_config.path,))

    EnvironmentError: mysql_config not found

    ----------------------------------------
    Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-3FRYWs/MySQL-python

我的setting.py文件是

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', 
        'NAME': 'poll_db',                     
        'USER': 'root',
        'PASSWORD': 'root',
    }
}

2 个答案:

答案 0 :(得分:1)

在上面的示例中,答案可能位于正确的日志文件pip.log/tmp/pip-build-rHe_Sb/MySQL-python中的某个位置。

1)使用PIP安装MySQL模块时,请确保已在系统中安装了mysql-devel(yum)或libmysqlclient-dev(apt-get)软件包。

2)顺便说一下,我建议使用mysqlclient$ pip install mysqlclient)而不是MySQL-python,因为它支持Python 2和3.x.

答案 1 :(得分:0)

您有两个选项,如下所述:

分发包是 - :

  

sudo apt-get install python-mysqldb

在这种情况下,您不能使用virtualenv no-site-packages(默认选项),但必须使用:

  

virtualenv --system-site-packages myenv

使用clean virtualenv并构建自己的python-mysql包。

首先创建virtualenv:

  

virtualenv myvirtualenv

     

source myvirtualenv / bin / activate

然后安装构建依赖项:

  

sudo apt-get build-dep python-mysqldb

现在你可以安装python-mysql

  

pip install mysql-python

注意Ubuntu包是python-mysql db ,python pypi包是python-mysql(没有db)