django - Error loading MySQLdb module: No module named MySQLdb

非 Y 不嫁゛ 提交于 2019-11-28 04:38:39

问题


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?


回答1:


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.




回答2:


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!




回答3:


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

pip install 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




回答4:


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




回答5:


I configured djando w/ mysql the following way:

1) set DJANGO_SETTINGS_MODULE environment variable in .bashrc and source it

2) change settings.py pointed by this env_var and local_settings.py (if any) like this (for 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) install mysql dev libraries and header files, ensure that mysql is on PATH

sudo apt-get install libmysqlclient-dev

4) install python package for mysql

sudo pip install MySQL-python

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

python manage.py dbshell

or

django-admin dbshell

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

python manage.py syncdb


来源:https://stackoverflow.com/questions/12772687/django-error-loading-mysqldb-module-no-module-named-mysqldb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!