django - 加载MySQLdb模块时出错:没有名为MySQLdb的模块 [英] django - Error loading MySQLdb module: No module named MySQLdb
问题描述
pypm install mysql-python
安装了MySQL模块。 数据库引擎是 django.db.backends.mysql
。
导入MySQLdb
在交互式shell中。
.\manage.py syncdb
创建表没有问题。
但是,当我在浏览器中打开网站时,我收到加载MySQLdb模块时出错:没有名为MySQLdb的模块
:
环境:
请求方法:GET
请求URL:http:// whatever /
Django版本:1.4.1
Python版本:2.7.2
已安装应用程序:
('django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles')
安装的中间件:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.mid dleware.csrf.CsrfViewMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
追溯:
文件C:\Python27\ get_response
89. response = middleware_method(request)
文件C:\Python27\lib\\中的lib \site-packages\django\core\handlers\base.py \\ site-packages\django\contrib\sessions\middleware.pyin process_request
10. engine = import_module(settings.SESSION_ENGINE)
文件C:\Python27\lib\\ import_module
中的\\site-packages\django\utils\importlib.py。_ _import __(name)
文件C:\Python27\lib\site- packages\django < module>中的\contrib\sessions\backends\cached_db.py
6.从django.contrib.sessions.backends.db导入SessionStore作为DBStore
文件C:\Python27\lib\site-packages\django\contrib\sessions\ < module>中的backends\db.py
3.从django.db import IntegrityError,transaction,router
文件C:\Python27\lib\site-packages\django\db\__init __。pyin<模块>
40. backend = load_backend(connection.settings_dict ['ENGINE'])
文件C:\Python27\lib\site-packages\django\db\__init __。py在__getattr__
34. return getattr(connections [DEFAULT_DB_ALIAS],item)
文件C:\Python27\lib\site-packages\django\db\utils.py __getitem__
92. backend = load_backend(db ['ENGINE'])
文件C:\Python27\lib\site-packages\django\db\utils.py load_backend
24. return import_module('。base',backend_name)
在import_module中的文件C:\Python27\lib\site- packages\django\utils\importlib.py
35. __import __(name)
文件C:\Python27\lib\site- packages\django\db\backends\mysql\base.py在<模块>
16. raise UnsperlyConfigured(Error loading MySQLdb module:%s%e)
异常类型:/
异常配置异常值:加载MySQLdb模块时出错:没有模块命名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卸载mysql-python
然后重新安装它全局使用 pypm -g install mysql-python
(注意 -g
选项)。
替代方法是将该路径添加到中的列表
sys.path.append(... path ...)
wsgi.py
所以,如果有人想知道,你可以找到MySQLdb(或任何其他模块)的安装位置,如下所示:
import MySQLdb
打印MySQLdb .__ file__
确保该路径位于Django错误消息中提供的Python路径列表中。
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
.
The database engine is django.db.backends.mysql
.
import MySQLdb
works in the interactive shell.
.\manage.py syncdb
created the tables with no problem.
However, when I open the site in the browser, I get 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?
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-python
and then reinstalled it globally using pypm -g install mysql-python
(note the -g
option).
The alternative is to add that path to the list sys.path.append("...path...")
in wsgi.py
So, in case someone else is wondering, you can find out where MySQLdb (or any other module) is installed like so:
import MySQLdb
print MySQLdb.__file__
Make sure that that path is in Python's path list provided in Django's error message.
这篇关于django - 加载MySQLdb模块时出错:没有名为MySQLdb的模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!