Reputation: 41256
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.
}
}
Upvotes: 18
Views: 14436
Reputation: 1041
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.
Upvotes: 1
Reputation: 9611
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
Upvotes: 2
Reputation: 85095
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.
Upvotes: 38
Reputation: 41
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.
Upvotes: 0
Reputation: 846
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/
Upvotes: 0
Reputation: 841
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.
Upvotes: 1