2
votes

So I am pretty sure that I have managed to dork up my MySQLdb installation. I have all of the following installed correctly on a fresh install of OS X Lion:

  1. phpMyAdmin

  2. MySQL 5.5.16

  3. Django 1.3.1

And yet when I try to run "from django.db import connection" in a django console, I get the following:

from django.db import connection Traceback (most recent call last): File "", line 1, in File "/Library/Python/2.7/site-packages/Django-1.3.1-py2.7.egg/django/db/init.py", line 78, in connection = connections[DEFAULT_DB_ALIAS] File "/Library/Python/2.7/site-packages/Django-1.3.1-py2.7.egg/django/db/utils.py", line 93, in getitem backend = load_backend(db['ENGINE']) File "/Library/Python/2.7/site-packages/Django-1.3.1-py2.7.egg/django/db/utils.py", line 33, in load_backend return import_module('.base', backend_name) File "/Library/Python/2.7/site-packages/Django-1.3.1-py2.7.egg/django/utils/importlib.py", line 35, in import_module import(name) File "/Library/Python/2.7/site-packages/Django-1.3.1-py2.7.egg/django/db/backends/mysql/base.py", line 14, in raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e) ImproperlyConfigured: Error loading MySQLdb module: dlopen(/Users/[my username]/.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/[my username]/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.7-intel.egg-tmp/_mysql.so Reason: image not found

I have no idea why this is happening, could somebody help walk me through this?

3

3 Answers

3
votes

Easy,

edit your .bash_profile (vi ~/.bash_profile) somewhere in that add the following line:

export DYLD_LIBRARY_PATH="/usr/local/mysql/lib"

This line assumes your mysql install directory is in /usr/local/mysql/.

This will solve executing via python interrupter launched in shell (the .bash_profile exports the path needed by the MySQLdb module to load the ' libmysqlclient.18.dylib').

If you are having this issue with a Python IDE like PyCharm add the DYLD_LIBRARY_PATH variable to the launching module configuration.

I hope this helps :)

Also,

To fully understand this problem, read the following section:

http://mysql-python.sourceforge.net/FAQ.html#importerror

This explains this error in detail

5
votes

I found the following solution for this issue. It worked for me. I have encountered this problem when I was running python console from PyCharm.

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

1
votes

Install pip if you haven't already, and run

pip install MySQL-Python