关于python:django中的“没有模块名称mysqldb”错误

“No module name mysqldb” error in django

本问题已经有最佳答案,请猛点这里访问。

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

python manage.py migrate

我的错误低于

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
(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

那我就少犯错了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
    (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文件是

1
2
3
4
5
6
7
8
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'poll_db',                    
        'USER': 'root',
        'PASSWORD': 'root',
    }
}


在上面的例子中,答案可能在正确的日志文件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。


您有两种选择,如下所述:

分发包是-:

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)