0

First of all I installed django on my Ubuntu machine

sudo apt-get install python-django

After that i created my first project using following command and a directory named 'mysite' was created.

django-admin.py startproject mysite

Here, running the following command starts a server and i checked in the browser, minimal server that comes with django was perfectly.

python manage.py runserver

Then, I installed mysql-server running this command

sudo apt-get install mysql-server

After this i create database in mysql named 'django_first' .
And used the following command to install python module mysqldb successfully.

sudo apt-get install python-mysqldb

As of now no issue occurred. But as soon as I run this command in the directory after setting all the necessary fields in settings.py file

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'django_first',
        'USER': 'root',
        'PASSWORD': 'password',
        'HOST': '',
        'PORT': '',
}

Now run this command python manage.py runserver

I get the following errors :-

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line  399, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 242, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 280, in execute
    translation.activate('en-us')
  File "/usr/local/lib/python2.7/site-packages/django/utils/translation/__init__.py", line 130, in activate
    return _trans.activate(language)
  File "/usr/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 188, in activate
    _active.value = translation(language)
  File "/usr/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py",     line 177, in translation
    default_translation = _fetch(settings.LANGUAGE_CODE)    
  File "/usr/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 159, in _fetch
    app = import_module(appname)
  File "/usr/local/lib/python2.7/site-packages/django/utils/importlib.py", line 40, in     import_module
    __import__(name)
  File "/usr/local/lib/python2.7/site-packages/django/contrib/admin/__init__.py", line 6, in <module>
    from django.contrib.admin.sites import AdminSite, site
  File "/usr/local/lib/python2.7/site-packages/django/contrib/admin/sites.py", line 4,     in <module>
    from django.contrib.admin.forms import AdminAuthenticationForm
  File "/usr/local/lib/python2.7/site-packages/django/contrib/admin/forms.py", line 6, in <module>
    from django.contrib.auth.forms import AuthenticationForm
  File "/usr/local/lib/python2.7/site-packages/django/contrib/auth/forms.py", line 17, in <module>
    from django.contrib.auth.models import User
  File "/usr/local/lib/python2.7/site-packages/django/contrib/auth/models.py", line 48, in <module>
    class Permission(models.Model):
  File "/usr/local/lib/python2.7/site-packages/django/db/models/base.py", line 96, in     __new__
    new_class.add_to_class('_meta', Options(meta, **kwargs))
  File "/usr/local/lib/python2.7/site-packages/django/db/models/base.py", line 264, in     add_to_class
    value.contribute_to_class(cls, name)
  File "/usr/local/lib/python2.7/site-packages/django/db/models/options.py", line 124, in contribute_to_class
    self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
  File "/usr/local/lib/python2.7/site-packages/django/db/__init__.py", line 34, in     __getattr__
    return getattr(connections[DEFAULT_DB_ALIAS], item)
  File "/usr/local/lib/python2.7/site-packages/django/db/utils.py", line 198, in     __getitem__
    backend = load_backend(db['ENGINE'])  
  File "/usr/local/lib/python2.7/site-packages/django/db/utils.py", line 113, in     load_backend
    return import_module('%s.base' % backend_name)
  File "/usr/local/lib/python2.7/site-packages/django/utils/importlib.py", line 40, in     import_module
    __import__(name)
  File "/usr/local/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line     17, in <module>
    raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module     named MySQLdb`

Looks to be some issue with mysqldb module not installed, but that's not exactly it is as I rerun the installation command for this.

Any ideas about this ?

3
  • try import MySQLdb in python console (not a django shell), what happens? Commented Feb 8, 2014 at 16:26
  • "ImportError: No module named MySQLdb" is what I am getting when I do import MySQLdb Commented Feb 8, 2014 at 16:38
  • oh wait never mind I see the problem. Commented Feb 8, 2014 at 16:55

3 Answers 3

1

MySQLdb is just the module that connects to MySQL, but it's part of a larger package called MySQL-Python. Just do:

pip install MySQL-python

And it should take care of everything (you probably need to sudo that command). In fact, I'd probably suggest you do:

pip install MySQL-python --upgrade

To assure it installs the latest version. If that doesn't change anything, follow these instructions to make sure you get the latest version and all required dependencies

p.s. in general, always prefer pip over apt-get for installing python packages, since it gets updated a lot more frequently. For example, the django version you have installed using the apt-get method might very well be a very old release. To fix that, delete the django folder in your site-packages and do:

pip install django==1.6.2 #this is the latest version
Sign up to request clarification or add additional context in comments.

2 Comments

As you said it was the problem with Mysqldb , but it didn't go with just upgrading MySQL-python . I was still facing the import errors. So i just built the MySQL source on my system and got it done
Sounds painful, but glad to hear it got sorted out
1

After trying all the suggestions given above, I managed to pinpoint the problem of no link to MySQLdb module.

I decided to build the code and installed the source from here. http://sourceforge.net/projects/mysql-python/

Before building it you need to ensure the below prerequisites:

  1. gcc compiler
  2. MySQL Server. I used the MySQL Community Server Version

Run these commands as well:

sudo apt-get install python-setuptools python-dev libmysqlclient15-dev

If you are getting error with mysql_config, do not forget to fix PATH env. variable

export PATH=$PATH:/usr/local/mysql/bin

Now we can compile the code:-

sudo python setup.py build
sudo python setup.py install

Now open your python command line and import the new MySQLdb module

$> python
>>> import MySQLdb

You should not see any import error.

Comments

0

Check if libmysqlclient-dev is installed or not? If it is not installed try to install

sudo apt-get install libmysqlclient-dev

Hope this will fix your issue

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.