pip install MySQL-python –upgrade
这个时候就会弹出这样的错误如下
Python mysqldb: Library not loaded: libmysqlclient.18.dylib
很显然,是说找不到mysql的library,
我们知道mysql的lib应该在/usr/local/mysql/lib下面,但是为何找不到
mac上lib的加载路径靠环境变量DYLD_LIBRARY_PATH
如下添加环境变量即可。
export DYLD_LIBRARY_PATH=/usr/local/mysql/lib/:$DYLD_LIBRARY_PATH
% 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)
发现_mysql.so依赖libmysqlclient.18.dylib没有路径信息, 使用如下命令添加路径信息
% sudo install_name_tool -change libmysqlclient.18.dylib /usr/local/mysql/lib/libmysqlclient.18.dylib /Library/Python/2.7/site-packages/_mysql.so
然后再次查看依赖, 发现此时已经有了lib的路径信息,直接运行,即可成功
% 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)
如果这篇文章能帮助到您,对您有用,那就打赏小弟一瓶饮料吧。