Python django - 加载 MySQLdb 模块时出错:没有名为 MySQLdb 的模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12772687/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:51:13  来源:igfitidea点击:

django - Error loading MySQLdb module: No module named MySQLdb

pythondjangomysql-python

提问by Alexei

I'm using Django 1.4.1 with Active Python 2.7 on Win7. I have installed the MySQL module using pypm install mysql-python.

我在 Win7 上使用 Django 1.4.1 和 Active Python 2.7。我已经使用pypm install mysql-python.

The database engine is django.db.backends.mysql.

数据库引擎是django.db.backends.mysql.

import MySQLdbworks in the interactive shell.

import MySQLdb在交互式 shell 中工作。

.\manage.py syncdbcreated the tables with no problem.

.\manage.py syncdb创建表没有问题。

However, when I open the site in the browser, I get Error loading MySQLdb module: No module named MySQLdb:

但是,当我在浏览器中打开网站时,我得到Error loading MySQLdb module: No module named MySQLdb

Environment:


Request Method: GET
Request URL: http://whatever/

Django Version: 1.4.1
Python Version: 2.7.2
Installed Applications:
('django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware')


Traceback:
File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
  89.                     response = middleware_method(request)
File "C:\Python27\lib\site-packages\django\contrib\sessions\middleware.py" in process_request
  10.         engine = import_module(settings.SESSION_ENGINE)
File "C:\Python27\lib\site-packages\django\utils\importlib.py" in import_module
  35.     __import__(name)
File "C:\Python27\lib\site-packages\django\contrib\sessions\backends\cached_db.py" in <module>
  6. from django.contrib.sessions.backends.db import SessionStore as DBStore
File "C:\Python27\lib\site-packages\django\contrib\sessions\backends\db.py" in <module>
  3. from django.db import IntegrityError, transaction, router
File "C:\Python27\lib\site-packages\django\db\__init__.py" in <module>
  40. backend = load_backend(connection.settings_dict['ENGINE'])
File "C:\Python27\lib\site-packages\django\db\__init__.py" in __getattr__
  34.         return getattr(connections[DEFAULT_DB_ALIAS], item)
File "C:\Python27\lib\site-packages\django\db\utils.py" in __getitem__
  92.         backend = load_backend(db['ENGINE'])
File "C:\Python27\lib\site-packages\django\db\utils.py" in load_backend
  24.         return import_module('.base', backend_name)
File "C:\Python27\lib\site-packages\django\utils\importlib.py" in import_module
  35.     __import__(name)
File "C:\Python27\lib\site-packages\django\db\backends\mysql\base.py" in <module>
  16.     raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)

Exception Type: ImproperlyConfigured at /
Exception Value: Error loading MySQLdb module: No module named MySQLdb

The settings for the sessions and messages apps are:

会话和消息应用程序的设置是:

SESSION_ENGINE = "django.contrib.sessions.backends.cached_db"
MESSAGE_STORAGE = "django.contrib.messages.storage.cookie.CookieStorage"

How is this possible?

这怎么可能?

回答by Alexei

The problem was that MySQLdb was installed in my home directory C:\Users\alexei\AppData\Roaming\Python\Python27\site-packages\which was not in Python's path. So I uninstalled it with pypm uninstall mysql-pythonand then reinstalled it globallyusing pypm -g install mysql-python(note the -goption).

问题是 MySQLdb 安装在我的主目录中C:\Users\alexei\AppData\Roaming\Python\Python27\site-packages\,而该目录不在 Python 的路径中。所以我卸载了它,pypm uninstall mysql-python然后使用(注意选项)全局重新安装它。pypm -g install mysql-python-g

The alternative is to add that path to the list sys.path.append("...path...")in wsgi.py

另一种方法是将该路径添加到列表sys.path.append("...path...")wsgi.py

So, in case someone else is wondering, you can find out where MySQLdb (or any other module) is installed like so:

因此,如果其他人想知道,您可以像这样找出 MySQLdb(或任何其他模块)的安装位置:

import MySQLdb
print MySQLdb.__file__

Make sure that that path is in Python's path list provided in Django's error message.

确保该路径在 Django 错误消息中提供的 Python 路径列表中。

回答by errakeshpd

i also faced this issue then i installed python mysql to my system

我也遇到了这个问题然后我在我的系统中安装了 python mysql

pip install mysql-python

pip 安装 mysql-python

then its workes see this--> working in my system >>
$ python manage.py syncdb;

Creating tables ...
Creating table django_admin_log
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
.............working

然后它的工作 看到这个--> 在我的系统中工作 >>
$ python manage.py syncdb;

创建表...
创建表 django_admin_log
创建表 auth_permission
创建表 auth_group_permissions
创建表 auth_group
........working

回答by tomermes

Easiest way is to install MySQLdb from binary: http://www.lfd.uci.edu/~gohlke/pythonlibs/#mysql-python

最简单的方法是从二进制安装 MySQLdb:http://www.lfd.uci.edu/~gohlke/pythonlibs/#mysql-python

回答by user7828

If you are using and virtual environment. you must run

如果您使用的是虚拟环境。你必须跑

cd virtualEnvPath
pip install MySQL-Python

Don't forget positioning inside of your virtual env. /bin PATH, to use local pip. REGARDS!

不要忘记在虚拟环境中定位。/bin PATH,使用本地 pip。问候!

回答by sdd

I configured djando w/ mysql the following way:

我通过以下方式配置了带有 mysql 的 djando:

1) set DJANGO_SETTINGS_MODULEenvironment variable in .bashrc and source it

1)DJANGO_SETTINGS_MODULE在 .bashrc 中设置环境变量并获取它

2) change settings.py pointed by this env_var and local_settings.py (if any) like this (for mysql):

2)像这样(对于mysql)更改此env_var和local_settings.py(如果有)指向的settings.py:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', 
        'NAME': 'DB_NAME',
        'USER': 'DB_USER',
        'PASSWORD': 'DB_PASSWORD',
        'HOST': 'localhost',   # Or an IP Address that your DB is hosted on
        'PORT': '3306',
    }
}

3) install mysql dev libraries and header files, ensure that mysql is on PATH

3)安装mysql dev库和头文件,确保mysql在PATH上

sudo apt-get install libmysqlclient-dev

4) install python package for mysql

4)为mysql安装python包

sudo pip install MySQL-python

5) test django's access to your mysql db:

5) 测试 django 对你的 mysql 数据库的访问:

python manage.py dbshell

or

或者

django-admin dbshell

6) sync django to mysql from dir with your django app

6) 使用 django 应用程序将 django 从目录同步到 mysql

python manage.py syncdb