Join the Stack Overflow Community
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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.
  }
}
share|improve this question
    
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 Tzvi 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 stackoverflow.com/questions/4730787/… – Ned Deily Mar 28 '12 at 17:36
up vote 34 down vote accepted

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.

share|improve this answer
    
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
    
Symlink worked for me as well. Thanks. – Stealth Aug 11 '12 at 19:12
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
    
Ned Daily & @beta, you rock, thanks! – mkoistinen Apr 2 '13 at 23:32

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
share|improve this answer

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.

share|improve this answer
    
MySQL is running fine for Ruby/PHP dev and that command shows the library in my mysql/lib path. Thanks. – Rob Wilkerson Mar 28 '12 at 17:29

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.

share|improve this answer

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/

share|improve this answer
    
I've added my db settings to the original question. Thanks. – Rob Wilkerson Mar 28 '12 at 17:34

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.