Python mysqldb: Library not loaded: libmysqlclient.18.dylib
I just compiled and installed mysqldb for python 2.7 on my mac os 10.6. I created a simple test file that imports
import MySQLdb as mysql
Firstly, this command is red underlined and the info tells me "Unresolved import". Then I tried to run the following simple python code
import MySQLdb as mysql
def main():
conn = mysql.connect( charset="utf8", use_unicode=True, host="localhost",user="root", passwd="",db="" )
if __name__ == '__main__'():
main()
When executing it I get the following error message
Traceback (most recent call last):
File "/path/to/project/Python/src/cvdv/TestMySQLdb.py", line 4, in <module>
import MySQLdb as mysql
File "build/bdist.macosx-10.6-intel/egg/MySQLdb/__init__.py", line 19, in <module>
namespace cvdv
File "build/bdist.macosx-10.6-intel/egg/_mysql.py", line 7, in <module>
File "build/bdist.macosx-10.6-intel/egg/_mysql.py", line 6, in __bootstrap__
ImportError: dlopen(/Users/toom/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.6-intel.egg-tmp/_mysql.so, 2): Library not loaded: libmysqlclient.18.dylib
Referenced from: /Users/toom/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.6-intel.egg-tmp/_mysql.so
Reason: image not found
What might be the solution to my problem?
EDIT: Actually I found out that the library lies in /usr/local/mysql/lib. So I need to tell my pydev eclipse version where to find it. Where do I set this?
I solved the problem by creating a symbolic link to the library. Ie
The actual library resides in
/usr/local/mysql/lib
And then I created a symbolic link in
/usr/lib
Using the command:
sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib
so that I have the following mapping:
ls -l libmysqlclient.18.dylib
lrwxr-xr-x 1 root wheel 44 16 Jul 14:01 libmysqlclient.18.dylib -> /usr/local/mysql/lib/libmysqlclient.18.dylib
That was it. After that everything worked fine.
EDIT:
Notice, that since MacOS El Capitan the System Integrity Protection (SIP, also known as "rootless") will prevent you from creating links in /usr/lib/
. You could disable SIP by following these instructions, but you can create a link in /usr/local/lib/
instead:
sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/local/lib/libmysqlclient.18.dylib
My preferred method is to actually fix the library rather than playing with environment variables that may or may not actually be in scope depending on how the application is run. This is actually a fairly simple process.
First, look at the error output to see where the offending python module is located:
ImportError: dlopen(/Library/Python/2.7/site-packages/_mysql.so, 2): Library not loaded: libmysqlclient.18.dylib Referenced from: /Library/Python/2.7/site-packages/_mysql.so Reason: image not found
Okay, so the offending file is /Library/Python/2.7/site-packages/_mysql.so
Next, figure out where _mysql.so thinks it should find libmysqlclient.18.dylib:
% otool -L /Library/Python/2.7/site-packages/_mysql.so
/Library/Python/2.7/site-packages/_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 169.3.0)
So, it's looking for libmysqlclient.18.dylib with no path information, let's fix that:
% sudo install_name_tool -change libmysqlclient.18.dylib /usr/local/mysql/lib/libmysqlclient.18.dylib /Library/Python/2.7/site-packages/_mysql.so
Now _mysql.so knows the full path to the library and everything works, regardless of environment variables.
% otool -L /Library/Python/2.7/site-packages/_mysql.so
/Library/Python/2.7/site-packages/_mysql.so:
/usr/local/mysql/lib/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 169.3.0)
I found there was another solution for this problem rather than creating a symbolic link.
You set the path to your directory, where libmysqlclient.18.dylib resides, to DYLD_LIBRARY_PATH environment variable. What I did is to put following line in my .bash_profile:
export DYLD_LIBRARY_PATH=/usr/local/mysql-5.5.15-osx10.6-x86/lib/:$DYLD_LIBRARY_PATH
That's it.
链接地址: http://www.djcxy.com/p/67888.html上一篇: 使用pydev和.pth文件导入