Charming Python and the mysterious case of libmysqlclient.18.dylib

I’m quite new to Python, virtualenv and other related animals on Mac, but so far I was able to create new virtual environments without much fuss… until yesterday. Two weeks ago I had to prepare an environment with MySQLdb. I installed it:

pip install MySQLdb-python

and thought “This is it”. It was definitely not. When I tried to import MySQLdb into Python, I got this error:

Error loading MySQLdb module:
dlopen(/Users/mateja/.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/mateja/.python-eggs/
MySQL_python-1.2.3-py2.7-macosx-10.7-intel.egg-tmp/_mysql.so
Reason: image not found

What the heck? After half an hour of googling I found a solution…
I edited my .bash_profile and added this line in it (note: I do have mysql installed in /usr/local/mysql/):

export DYLD_LIBRARY_PATH=/usr/local/mysql/lib

This worked like a charm… in a console, but not in PyCharm IDE. Fortunately, this answer also provided a very helpful hint…
I had to add DYLD_LIBRARY_PATH variable to launching module configuration (Run->Edit configurations). Guess what? This time it worked like a charm in PyCharm as well :)
Note: PyCharm requires configuration per script… I have to check if there is a “global” configuration…

Anyway, go me! Viva la Stackoverflow!

Enhanced by Zemanta