13

I'm trying start up a Django project.

I get this error on trying to run the manage.py:

(venv)dyn-160-39-161-214:proj Bren$ python manage.py
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/Bren/Desktop/fss/venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 354, in execute_from_command_line
    utility.execute()
  File "/Users/Bren/Desktop/fss/venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 328, in execute
    django.setup()
  File "/Users/Bren/Desktop/fss/venv/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/Bren/Desktop/fss/venv/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "/Users/Bren/Desktop/fss/venv/lib/python2.7/site-packages/django/apps/config.py", line 198, in import_models
    self.models_module = import_module(models_module_name)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/Users/Bren/Desktop/fss/venv/lib/python2.7/site-packages/django/contrib/auth/models.py", line 41, in <module>
    class Permission(models.Model):
  File "/Users/Bren/Desktop/fss/venv/lib/python2.7/site-packages/django/db/models/base.py", line 139, in __new__
    new_class.add_to_class('_meta', Options(meta, **kwargs))
  File "/Users/Bren/Desktop/fss/venv/lib/python2.7/site-packages/django/db/models/base.py", line 324, in add_to_class
    value.contribute_to_class(cls, name)
  File "/Users/Bren/Desktop/fss/venv/lib/python2.7/site-packages/django/db/models/options.py", line 250, in contribute_to_class
    self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
  File "/Users/Bren/Desktop/fss/venv/lib/python2.7/site-packages/django/db/__init__.py", line 36, in __getattr__
    return getattr(connections[DEFAULT_DB_ALIAS], item)
  File "/Users/Bren/Desktop/fss/venv/lib/python2.7/site-packages/django/db/utils.py", line 241, in __getitem__
    backend = load_backend(db['ENGINE'])
  File "/Users/Bren/Desktop/fss/venv/lib/python2.7/site-packages/django/db/utils.py", line 112, in load_backend
    return import_module('%s.base' % backend_name)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/Users/Bren/Desktop/fss/venv/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 27, in <module>
    raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb

I was wondering what module I should try to install.

When I use pip install MySQLdb I get this error:

    Collecting MySQLdb
  Could not find a version that satisfies the requirement MySQLdb (from versions: )
No matching distribution found for MySQLdb

When I look this up on SO and other places it seems there are lots of different mysql modules out there.

Also this is what my setup looks like for the DB (I didnt write this porject and am largely new to django and python):

    DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',

...

    }
}

EdBaker suggested pip install MySQL-python

I got this as a response:

Collecting MySQL-python   Using cached MySQL-python-1.2.5.zip
    Complete output from command python setup.py egg_info:
    sh: mysql_config: command not found
    Traceback (most recent call last):
      File "<string>", line 20, in <module>
      File "/private/var/folders/m3/11zknyw55zxbw6zqh58rwq580000gn/T/pip-build-DC9tSL/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 /private/var/folders/m3/11zknyw55zxbw6zqh58rwq580000gn/T/pip-build-DC9tSL/MySQL-python

Attempting to run import MySQLdb in the shell:

[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named MySQLdb

Attempting to upgrade pip:

pip install pip --upgrade

showed pip was up to date

Requirement already up-to-date: pip in /Users/Bren/Desktop/fss/venv/lib/python2.7/site-packages
Bren
  • 3,516
  • 11
  • 41
  • 73
  • Try the answer to this: https://stackoverflow.com/questions/25865270/how-to-install-python-mysqldb-module-using-pip – Ed Baker Dec 01 '15 at 21:13
  • @EdBaker Didn't work will update with the error. – Bren Dec 01 '15 at 21:16
  • Can you attempt to bring up a python shell and run add "import MySQLdb" .. that way we can figure out if it's a python error or a django one – Ed Baker Dec 01 '15 at 21:19
  • 1
    Try and update pip first: "pip install pip --upgrade" then try "pip install MySQL-python" – Ed Baker Dec 01 '15 at 21:22
  • @EdBaker Done and done didnt help unfortunately. (Post has details) – Bren Dec 01 '15 at 21:26
  • Are you sure that it's a good idea to post the credentials on SO? I would change them if I were you.. – fodma1 Dec 01 '15 at 21:27
  • Clearly installing `pip install MySQL-python` is the way to go, I would try the things listed here: http://stackoverflow.com/questions/7475223/mysql-config-not-found-when-installing-mysqldb-python-interface depending on your OS – fodma1 Dec 01 '15 at 21:35
  • After looking around it looks like there are issues with the mysql build path, you may need to install libmysqlclient-dev to get this right, on ubuntu the command is "apt-get install python-dev libmysqlclient-dev" .. then have another shot at pip install blaa – Ed Baker Dec 01 '15 at 21:39
  • @fodma1 I'm stuck on my mac so the apt-get won't work for me and there isn't an equivalent in homebrew. I don't suppose either of you know what a possible solution is for macs? I'm going to try installing fink but thought I would ask. – Bren Dec 01 '15 at 21:47
  • @EdBaker I'm stuck on my mac so the apt-get won't work for me and there isn't an equivalent in homebrew. I don't suppose either of you know what a possible solution is for macs? I'm going to try installing fink but thought I would ask. – Bren Dec 01 '15 at 21:47
  • 1
    @Bren This answer is exactly for Mac: http://stackoverflow.com/a/33928783/2419215 – fodma1 Dec 01 '15 at 21:48
  • Another link to help you out https://stackoverflow.com/questions/25459386/mac-os-x-environmenterror-mysql-config-not-found – Ed Baker Dec 01 '15 at 22:01
  • @Bren just posted it – fodma1 Dec 02 '15 at 06:37
  • you can also try this https://stackoverflow.com/a/51811349/1552410 – kaya Sep 08 '18 at 23:28

4 Answers4

29

Clearly installing pip install MySQL-python is the way to go. The problem is with the location of mysql_config. Look around here, this might help you depending on your OS: mysql_config not found when installing mysqldb python interface

Edit:

In case the install script fails with Command "python setup.py egg_info" failed with error code 1 in {...}, installing libmysqlclient-dev with

sudo apt install libmysqlclient-dev

should fix the issue. Thanks to @thirupathi-thangavel.

fodma1
  • 3,485
  • 1
  • 29
  • 49
2

If the above method not working then try

mysqlclient

pip install mysqlclient

I did not found the above library using python version(3.9) and I installed the mysqlclient and it worked for fine.

1

brew install mysql

Path will be shown after installation is complete, use that in below command :

sudo PATH=/usr/local/Cellar/mysql/bin/:$PATH pip install mysql-python
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
0

I followed my question here 404 error when using brew to install mysql

and then as fodma1 suggested went to here stackoverflow.com/a/33928783/2419215

and now it works for the most part (have another error but thats for another post)!

Thanks EdBaker and fodma1!

Community
  • 1
  • 1
Bren
  • 3,516
  • 11
  • 41
  • 73
  • If @Edbaker or @fodma1! want to make one of their comments I'll accept it as the answer. – Bren Dec 01 '15 at 23:55