Django syncdb throwing No module named MySQLdb


I am a newbie learning Python/Django...

Am using the following tutorial located here.

Created a mysite database in MySQL 5 running on Snow Leopard.

Edited the file to look like this:

DATABASE_ENGINE = 'mysql'        
DATABASE_NAME = 'mysite'             
DATABASE_USER = 'root'             
DATABASE_HOST = ''            
DATABASE_PORT = ''             

Now when I run the following command:

python syncdb

I receive the following error:

Traceback (most recent call last):
File "", line 11, in <module>
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
  python2.6/site-packages/django/core/management/", line 362,
  in execute_manager
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
  python2.6/site-packages/django/core/management/", line 303,
  in execute
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
  python2.6/site-packages/django/core/management/", line 195, in
  self.execute(*args, **options.__dict__)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
  python2.6/site-packages/django/core/management/", line 221, in
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
  python2.6/site-packages/django/core/management/", line 249, in
  num_errors = get_validation_errors(s, app)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
  python2.6/site-packages/django/core/management/", line
  22, in get_validation_errors
  from django.db import models, connection
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
  python2.6/site-packages/django/db/", line 41, in <module>
  backend = load_backend(settings.DATABASE_ENGINE)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages  
   /django/db/", line 17, in load_backend
   return import_module('.base', 'django.db.backends.%s' %backend_name)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
  python2.6/site-packages/django/utils/", line 35, in import_module
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages       /django/db/backends/mysql/", line 13, in <module>
   raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
   django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb
   module: No module named MySQLdb

What am I possibly doing wrong?

Happy programming...

8/17/2011 2:21:19 PM

sudo easy_install mysql-python

will install the MySQLdb module to allow you to work with MySQL from Python, or, if you want to work with virtualenv (which you should),

sudo easy_install virtualenv virtualenvwrapper
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper_bashrc
mkvirtualenv mysite
pip install mysql-python django

Will put you inside a virtualenv with a current install of django (you can specify which version, e.g. django==1.1.1) and the MySQLdb module installed. Using virtualenv will allow you to have separate environments for different projects so you can install different modules and even use different versions of those modules (or Python) for different projects. To leave you virtualenv just type the command


or, to switch to the environment 'foo' type

workon foo

You should also, if you're going to be using virtualenv add these three lines to your ~/.bash_profile (on OS X, ~/.bashrc generally on Linux):

export WORKON_HOME=$HOME/.virtualenvs # where virtualenvs should be created
export PIP_VIRTUALENV_BASE=$WORKON_HOME # tells pip where to look for virtualenvs
source /usr/local/bin/virtualenvwrapper_bashrc # bash completion and wrapper functions for virtualenv
11/17/2009 2:01:53 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow