Django - Error loading MySQLdb module: no module named MySQLdb

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

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

import MySQLdb runs in an interactive shell.

.\manage.py syncdb created the tables without problems.

However, when I open the site in a 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 

Application settings for sessions and messages:

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

How is this possible?

+4
source share
5 answers

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

An alternative is to add this path to the sys.path.append("...path...") wsgi.py in wsgi.py

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

 import MySQLdb print MySQLdb.__file__ 

Make sure this path is in the Python path list provided in the Django error message.

+3
source

If you are using a virtual environment. you have to run

 cd virtualEnvPath pip install MySQL-Python 

Do not forget to position inside your virtual env./bin PATH to use the local pip. REGARDS!

+2
source

I also ran into this problem, after which I installed python mysql on my system

pip install mysql-python

then his work see this -> it works in my system ->
$ python manage.py syncdb;
Creating tables ...
Creating the django_admin_log table
Creating the auth_permission table
Creating the auth_group_permissions table
Creating an auth_group table
............. worker

+1
source

I configured djando w / mysql as follows:

1) set the DJANGO_SETTINGS_MODULE environment DJANGO_SETTINGS_MODULE in .bashrc and enter it

2) change the settings.py indicated by this env_var and local_settings.py (if any) as it is (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, make sure mysql is in PATH

 sudo apt-get install libmysqlclient-dev 

4) install python package for mysql

 sudo pip install MySQL-python 

5) check django access to your mysql db:

 python manage.py dbshell 

or

 django-admin dbshell 

6) sync django with mysql from dir using django application

 python manage.py syncdb 
+1
source

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

0
source

Source: https://habr.com/ru/post/1438298/


All Articles