18

I'm starting down the path of learning Python/Django and have hit my first snag. When attempting to set my database in settings.py, the internal server fails with:

File "/Library/Python/2.7/site-packages/django/db/backends/mysql/base.py", line 16, in <module>
raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: dlopen(/Users/rob/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.7-intel.egg-tmp/_mysql.so, 2): Library not loaded: libmysqlclient.18.dylib
Referenced from: /Users/rob/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.7-intel.egg-tmp/_mysql.so
Reason: image not found

There are a lot of solutions that I've found, mostly involving the explicit definition of my DYLD_LIBRARY_PATH value, but that doesn't work for me. MysQL-Python is installed (v1.2.3).

Any idea what I might need to do to push through this?

Thanks.

UPDATE

DATABASES = {
  'default': {
    'ENGINE': 'django.db.backends.mysql',  # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
    'NAME': 'django_tutorial',                      # Or path to database file if using sqlite3.
    'USER': 'root',                      # Not used with sqlite3.
    'PASSWORD': '',                  # Not used with sqlite3.
    'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
    'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
  }
}
Ned Deily
  • 83,389
  • 16
  • 128
  • 151
Rob Wilkerson
  • 40,476
  • 42
  • 137
  • 192
  • for OSX, as general practice i'd recommend you to use the PyPM package manager & repository from ActiveState. I've used: 'sudo pypm install mysql-python' and (almost) never had an issue with packages – Mihai Oprea Mar 28 '12 at 16:13
  • 1
    If you're just trying to learn Django, it might be advantageous to start by just using SQLite. I start all of my projects in SQLite, then migrate them over to a real DB as necessary. – Naftuli Kay Mar 28 '12 at 16:25
  • @Mihai Oprea - I've used both pip and easy_install to check for the MySQL package. MySQL itself is working fine for both Ruby and PHP dev on the same machine. – Rob Wilkerson Mar 28 '12 at 17:32
  • @TK Kocheran - Maybe so, but I'm already familiar with MySQL and know I will use either it or PGSQL in the wild, so I figure I might as well work through the growing pains now. :-) – Rob Wilkerson Mar 28 '12 at 17:33
  • Duplicate of http://stackoverflow.com/questions/4730787/python-import-mysqldb-error-mac-10-6/4731333#4731333 – Ned Deily Mar 28 '12 at 17:36
  • Hey, Ned -- I did see your answer over there, but that solution doesn't seem to solve my problem (or I'm misunderstanding). The `otool` command shows the same lib file (libmysqlclient.18.dylib) in both cases. I'd really like to use my existing MySQL server instead of installing a new one, so I thought (read: hoped) there might be a more current solution. – Rob Wilkerson Mar 28 '12 at 17:52
  • Does it show *exactly* the same path for each including leading `/`? – Ned Deily Mar 28 '12 at 17:54
  • Well, now that you put it that way...no. No it does not. `$ otool -DX /usr/local/mysql/lib/libmysqlclient.18.dylib` --> `libmysqlclient.18.dylib`. `otool -L ~/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.7-intel.egg-tmp/_mysql.so` --> `/Users/rob/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.7-intel.egg-tmp/_mysql.so: libmysqlclient.18.dylib (compatibility version 18.0.0, current version 18.0.0) /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)` – Rob Wilkerson Mar 28 '12 at 18:03
  • @RobWilkerson Gotcha. Didn't know what the goal was, if it's to learn Django, take the past of least resistance in letting you get access to the APIs. – Naftuli Kay Mar 28 '12 at 18:32

6 Answers6

38

From your comments, it appears that the libmysqlclient dylib was installed with a non-absolute library name path. That's contrary to standard practice on OS X which is different from most other Unix-y systems in this respect. You should be able to permanently fix the problem (at least until your next upgrade) by modifying the path in the .so file by using install_name_tool or you can make it work by ensuring your Django instance is running with the following environment variable defined:

export DYLD_LIBRARY_PATH=/usr/local/mysql/lib

You also might be able to get it to work by creating a symlink in /usr/local/lib to the dylib in /usr/local/mysql/lib since /usr/local/lib is on the default dynamic load search path, so (untested!) something like:

sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/local/lib/libmysqlclient.18.dylib

A better long-term solution is to not use a broken MySQL client installation as suggested in Python import MySQLdb error - Mac 10.6.

Community
  • 1
  • 1
Ned Deily
  • 83,389
  • 16
  • 128
  • 151
  • Setting the library path doesn't seem to work for me. I was hesitant to use `install_name_tool` because it just sounded...destructive, but maybe that's my only hope if I want to use my existing, binary install of MySQL (and that would be preferable). Thanks. – Rob Wilkerson Mar 28 '12 at 18:33
  • The symlink worked (and feels less destructive to me), so that's how I'm rolling right now. Thanks for getting me over this particular hump. – Rob Wilkerson Mar 28 '12 at 18:55
  • 2
    works with --> sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/local/lib/libmysqlclient.18.dylib – kappatech Nov 23 '12 at 19:14
  • Brava answer. Save my time. – David Oct 31 '13 at 20:42
  • @Dae: Thanks for noticing the typo! Sorry, your proposed edit had already been rejected by the time I saw it. – Ned Deily Aug 11 '14 at 21:13
  • THANK YOU! Creating the symbolic link worked for me -- but I am actually installing mysql-simple in Haskell. Had the same problem -- image not found. – Eric Feb 13 '16 at 22:54
  • The symlink has worked! i have been scratching my head for an hour finding the solution to this! thank you so much! :) – Yejin Jul 16 '20 at 08:35
2

Using a homebrew installation of mysql, this worked for me:

$ mdfind libmysqlclient
/usr/local/Cellar/mysql/5.7.9/lib/libmysqlclient.20.dylib
/usr/local/Cellar/mysql/5.7.9/lib/libmysqlclient.a
/usr/local/Cellar/mysql/5.6.27/lib/libmysqlclient.18.dylib
/usr/local/Cellar/mysql/5.6.27/lib/libmysqlclient.a
/usr/local/Cellar/mysql/5.6.26/lib/libmysqlclient.18.dylib
/usr/local/Cellar/mysql/5.6.26/lib/libmysqlclient.a

$ sudo ln -s /usr/local/Cellar/mysql/5.6.27/lib/libmysqlclient.18.dylib /usr/local/lib/libmysqlclient.18.dylib
rebelliard
  • 9,592
  • 6
  • 47
  • 80
1

You probably have upgraded mysql recently. I got this issue when I upgraded MySql from 5.6 to 5.7 with brew. This definitely broke some libs, but relinking mysql back to 5.6 fixed the issue. So first see what mysql version you had previously with: brew info mysql and after switch back to that version using brew switch mysql 5.6.22 command for example.

Developer
  • 983
  • 12
  • 12
1

Make sure you have not just MySql-Python, but the actual MySql. MySQL-python is trying to load the mysql library, which should be somewhere like /usr/local/mysql-VERSION/lib.

Try running

mdfind libmysqlclient

on the command line. Scan through those results and if you don't see the file it's missing (libmysqlclient.18.dylib) then you probably don't have mysql installed properly.

RotaJota
  • 831
  • 9
  • 14
0

I don't have enough reputation to comment on Ned Deily's answer above :), but the environment variable method worked for me, after fixing the typo; it should be:

export DYLD_LIBRARY_PATH=/usr/local/mysql/lib

If you go that route I would suggest two things; make sure you don't bash any other DYLD_LIBRARY_PATH settings by using

export DYLD_LIBRARY_PATH=$DYLD_LIBARY_PATH:/usr/local/mysql/lib

and put it in your .bashrc file so you don't have to remember to set it in each terminal session.

The benefit to this method over the symlink option is that it will allow all the mysql dylib files to be found, not just the one(s) you specifically symlink.

barrett
  • 31
  • 2
0

Can you show us your settings.py code? you can also try edit the path to the mysql_config file to point to /usr/local/mysql/bin/mysql_config as discussed in better detail in this article: http://dakrauth.com/blog/entry/python-and-django-setup-mac-os-x-leopard/

Frantz Romain
  • 836
  • 1
  • 6
  • 14