繁体   English   中英

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

[英]django - Error loading MySQLdb module: No module named MySQLdb

我在Win7上使用Django 1.4.1和Active Python 2.7。 我使用pypm install mysql-python安装了MySQL模块。

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

import MySQLdb在交互式shell中工作。

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

但是,当我在浏览器中打开网站时,我收到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

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

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

这怎么可能?

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

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

所以,万一其他人想知道,你可以找到MySQLdb(或任何其他模块)的安装位置,如下所示:

import MySQLdb
print MySQLdb.__file__

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

如果您正在使用和虚拟环境。 你必须跑

cd virtualEnvPath
pip install MySQL-Python

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

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

pip install mysql-python

然后它的工作人员 看到 - > 在我的系统中工作>>
$ python manage.py syncdb;
创建表格......
创建表django_admin_log
创建表auth_permission
创建表auth_group_permissions
创建表auth_group
.............工作

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

我用以下方式配置了djando w / mysql:

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

2)更改此env_var和local_settings.py(如果有)指向的settings.py,如下所示(对于mysql):

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)安装mysql dev库和头文件,确保mysql在PATH上

sudo apt-get install libmysqlclient-dev

4)为mysql安装python包

sudo pip install MySQL-python

5)测试django对你的mysql db的访问:

python manage.py dbshell

要么

django-admin dbshell

6)用你的django应用程序从dir将django同步到mysql

python manage.py syncdb

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM