Whellow
Whellow

Reputation: 398

Django unable to find MySQLdb python module

Installed Django from source (python setup.py install and such), installed MySQLdb from source (python setup.py build, python setup.py install). Using Python 2.4 which came installed on the OS (CentOS 5.5). Getting the following error message after launching the server:

Error loading MySQLdb module: No module named MySQLdb

The pythonpath the debug info provides includes

'/usr/lib/python2.4/site-packages'

and yet, if I ls that directory, I can plainly see

MySQL_python-1.2.3-py2.4-linux-i686.egg

Using the python interactive shell, I can type import MySQLdb and it produces no errors. This leads me to believe it's a Django pathing issue, but I haven't the slightest clue where to start looking as I'm new to both Django and python.

EDIT: And to be a bit more specific, everything is currently running as root. I haven't setup any users yet on the machine, so none exist other than root.

EDITx2: And to be even more specific, web server is Cherokee, and deploying using uWSGI. All installed from source.

Upvotes: 19

Views: 38649

Answers (10)

user5997623
user5997623

Reputation: 1

This issue was the result of an incomplete/incorrect installation of the MySQL for Python adapter.
Specifically, I had to edit the path to the mysql_config file to point to /usr/local/mysql/bin/mysql_config.
Discussed in greater detail in this article:
http://dakrauth.com/blog/entry/python-and-django-setup-mac-os-x-leopard/

Upvotes: -1

Wagh
Wagh

Reputation: 4306

Try this if you are using linux:- sudo apt-get install python-mysqldb

windows:- pip install python-mysqldb or easy_install python-mysqldb

Hope this should work

Upvotes: 1

Burak
Burak

Reputation: 1

in my case, Python was able to access mySQL, but Django (1.6) gave the error: "Error loading MySQLdb module: No module named MySQLdb"

I am on a Macbook OSX (Maverick), by the way.

When I tried to run

pip install mysql-python

I got an error during compilation : "clang: error: unknown argument: ‘-mno-fused-madd’ [-Wunused-command-line-argument-hard-error-in-future]."

The problem, it turns out, is an updated behavior of cc compiler with the new Xcode 5.1. When there is a parameter it doesn't recognize, considers it as a fatal error and quits. The solution to override this behavior can be found here:

http://bruteforce.gr/bypassing-clang-error-unknown-argument.html

Upvotes: -1

Richardd
Richardd

Reputation: 1012

pip install mysql-python

Solved my problem :)

Upvotes: -1

digicyc
digicyc

Reputation: 406

I was having this same problem, but it was only an issue inside a virtualenv.

What I did to finally fix it was

workon [project_name]
pip uninstall django
pip install mysql-python
pip install django

So making sure you install mysql-python before django seems to work.
This is on a Ubuntu system and using virtualenv.

Upvotes: 1

kelorek
kelorek

Reputation: 6214

What worked for me (Linux Mint):

sudo apt-get install libmysqlclient-dev (this was the key for me)
pip install mysql-python
pip install django

Upvotes: 7

ebarch
ebarch

Reputation: 89

This did the trick for me:

sudo apt-get install python-dev

Upvotes: -1

vdboor
vdboor

Reputation: 22526

Have you considered installing MySQLdb from python packages? I would also recommend doing this with pip instead of easy_install.

First you can replace easy_install with pip:

easy_install pip
pip install pip --upgrade

And then install Django via PIP:

pip install MySQL-python
pip install Django

Typically easy_install is installed already (part of setuptools), while pip is much better. It offers uninstallation options too, and uses flat install directories instead of the EGG files magic. This might resolve some incompatibilities as well.

Upvotes: 33

Honza Pokorny
Honza Pokorny

Reputation: 3235

Did you try building the dependencies? This solved it for me on Ubuntu:

sudo apt-get build-dep python-mysqldb
pip install MySQLdb-python

Upvotes: 17

heckj
heckj

Reputation: 7367

You can find out where Python is looking for it's libraries by invoking "python manage.py shell" from the directory base of your Django project. Then do:

import sys
import pprint
pprint.pprint(sys.path)

And you'll see where the python is pulling libraries from. Also try to do a "import mysql" to see if that's kicking out an error.

Finally, the pathing for the WSGI service is (likely) configured with the uWSGI setup in Cherokee - sorry, I don't know the details of that critter to make suggestions on how to determine where/how it's loading the library path.

Upvotes: 1

Related Questions