1

i have install django 1.7(python 2.7.6) on ubantu 14.04 with mysql database engine but on running command-

python manage.py migrate

i am getting below error

(env1)shiv@shiv-Calpella:~/Project/Poll_app/mysite$ python manage.py migrate
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/shiv/Envs/env1/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/home/shiv/Envs/env1/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute
    django.setup()
  File "/home/shiv/Envs/env1/local/lib/python2.7/site-packages/django/__init__.py", line 21, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/shiv/Envs/env1/local/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "/home/shiv/Envs/env1/local/lib/python2.7/site-packages/django/apps/config.py", line 202, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/shiv/Envs/env1/local/lib/python2.7/site-packages/django/contrib/auth/models.py", line 40, in <module>
    class Permission(models.Model):
  File "/home/shiv/Envs/env1/local/lib/python2.7/site-packages/django/db/models/base.py", line 122, in __new__
    new_class.add_to_class('_meta', Options(meta, **kwargs))
  File "/home/shiv/Envs/env1/local/lib/python2.7/site-packages/django/db/models/base.py", line 297, in add_to_class
    value.contribute_to_class(cls, name)
  File "/home/shiv/Envs/env1/local/lib/python2.7/site-packages/django/db/models/options.py", line 166, in contribute_to_class
    self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
  File "/home/shiv/Envs/env1/local/lib/python2.7/site-packages/django/db/__init__.py", line 40, in __getattr__
    return getattr(connections[DEFAULT_DB_ALIAS], item)
  File "/home/shiv/Envs/env1/local/lib/python2.7/site-packages/django/db/utils.py", line 242, in __getitem__
    backend = load_backend(db['ENGINE'])
  File "/home/shiv/Envs/env1/local/lib/python2.7/site-packages/django/db/utils.py", line 108, in load_backend
    return import_module('%s.base' % backend_name)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/shiv/Envs/env1/local/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 18, in <module>
    raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb

i have tried installing mysqldb by commands

pip install MySQL-python

then i am getting below error

    (env1)shiv@shiv-Calpella:~/Project/Poll_app/mysite$ pip install MySQL-pythonCollecting MySQL-python
  Using cached MySQL-python-1.2.5.zip
    sh: 1: mysql_config: not found
    Traceback (most recent call last):
      File "<string>", line 20, in <module>
      File "/tmp/pip-build-3FRYWs/MySQL-python/setup.py", line 17, in <module>
        metadata, options = get_config()
      File "setup_posix.py", line 43, in get_config
        libs = mysql_config("libs_r")
      File "setup_posix.py", line 25, in mysql_config
        raise EnvironmentError("%s not found" % (mysql_config.path,))
    EnvironmentError: mysql_config not found
    Complete output from command python setup.py egg_info:
    sh: 1: mysql_config: not found

    Traceback (most recent call last):

      File "<string>", line 20, in <module>

      File "/tmp/pip-build-3FRYWs/MySQL-python/setup.py", line 17, in <module>

        metadata, options = get_config()

      File "setup_posix.py", line 43, in get_config

        libs = mysql_config("libs_r")

      File "setup_posix.py", line 25, in mysql_config

        raise EnvironmentError("%s not found" % (mysql_config.path,))

    EnvironmentError: mysql_config not found

    ----------------------------------------
    Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-3FRYWs/MySQL-python

my setting.py file is

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql', 
        'NAME': 'poll_db',                     
        'USER': 'root',
        'PASSWORD': 'root',
    }
}
  • What operating system are you using? – AndrewS Mar 14 '15 at 13:41
  • `Command "python setup.py egg_info" failed with error code 1` is not the real error message here. Look above this line for the actual error and add it to your question please. – Lukas Graf Mar 14 '15 at 13:45
  • http://stackoverflow.com/questions/7459766/installing-mysql-python – ndpu Mar 14 '15 at 13:52
  • @ShivDangi thank you. So the actual error is `EnvironmentError: mysql_config not found`. Which means `mysql_config` can't be found in your path, and the installation of `MySQL-python` fails because of that. The question I linked as a duplicate should help you to resolve this (as should @WojciechPolak's answer). – Lukas Graf Mar 14 '15 at 13:57

2 Answers2

1

In the above example, the answer might be in the proper log file pip.log or somewhere in /tmp/pip-build-rHe_Sb/MySQL-python.

1) When installing MySQL module with PIP, please make sure you have installed for instance the mysql-devel (yum) or libmysqlclient-dev (apt-get) package in your system.

2) BTW, I would recommend mysqlclient ($ pip install mysqlclient) instead of MySQL-python, as it supports both Python 2 and 3.x.

0

You have 2 options, as described bellow:

Distribution package are-:

sudo apt-get install python-mysqldb

In this case you can't use virtualenv no-site-packages (default option) but must use:

virtualenv --system-site-packages myenv

Use clean virtualenv and build your own python-mysql package.

First create virtualenv:

virtualenv myvirtualenv

source myvirtualenv/bin/activate

Then install build dependencies:

sudo apt-get build-dep python-mysqldb

Now you can install python-mysql

pip install mysql-python

NOTE Ubuntu package is python-mysqldb , python pypi package is python-mysql (without db)

Rahul Satal
  • 2,107
  • 3
  • 32
  • 53