简体   繁体   中英

Django manage.py 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 settings.py file to look like this:

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

Now when I run the following command:

python manage.py syncdb

I receive the following error:

Traceback (most recent call last):
File "manage.py", line 11, in <module>
  execute_manager(settings)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
  python2.6/site-packages/django/core/management/__init__.py", line 362,
  in execute_manager
  utility.execute()
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
  python2.6/site-packages/django/core/management/__init__.py", line 303,
  in execute
  self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
  python2.6/site-packages/django/core/management/base.py", line 195, in
  run_from_argv
  self.execute(*args, **options.__dict__)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
  python2.6/site-packages/django/core/management/base.py", line 221, in
  execute
  self.validate()
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
  python2.6/site-packages/django/core/management/base.py", line 249, in
  validate
  num_errors = get_validation_errors(s, app)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
  python2.6/site-packages/django/core/management/validation.py", 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/__init__.py", 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/__init__.py", 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/importlib.py", line 35, in import_module
  __import__(name)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages       /django/db/backends/mysql/base.py", 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...

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
export PIP_VIRTUALENV_BASE=$WORKON_HOME
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, eg 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

deactivate

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

The problem is that you are missing the Python module that interfaces with MySQL.

See the Django docs for get your database running which further points to MySQL notes .

However, if you are just going through the tutorial it is much easier to use SQLite backend which is built-in to Python. No drivers, server, etc. needed.

It's worth pointing out (even at this late date) that the "ImproperlyConfigured" error below

File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages /django/db/backends/mysql/base.py", line 13, in
raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e) django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb

can be a red herring in cases where mysql isn't running - I was just googling this error myself, wondering why a misconfiguration had 'suddenly' appeared and while searching and prodding I ran some other manage.py command and got this similar, but much more helpful, error:

File "~/.virtualenvs/mdid3/lib/python2.7/site-packages/MySQLdb/connections.py", line 187, in init

super(Connection, self). init (*args, **kwargs2)

_mysql_exceptions.OperationalError: (2003, "Can't connect to MySQL server on '127.0.0.1' (61)")

After checking mysqld, finding it not running and starting it -- my configuration error was miraculously fixed!

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

Make sure to create your database and with your MySQL server running try to connect to the database to check that database name, username and password are correct. then run:

1) pip install mysqlclient

2) python manage.py migrate

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM