Python:MySQLdb和“未加载库:libmysqlclient.16.dylib”

时间:2010-12-30 03:01:21

标签: python mysql macos osx-snow-leopard

设置......

尝试设置一个干净的Mac OS X 10.6安装来开发python / django,我不记得在10.5上遇到这个问题。

从安装程序mysql-5.5.8-osx10.6-x86_64.dmg安装MySQL后,我跑了

$ sudo pip install MySQL-python

它似乎顺利(下面的输出)

Downloading/unpacking MySQL-python
  Downloading MySQL-python-1.2.3.tar.gz (70Kb): 70Kb downloaded
  Running setup.py egg_info for package MySQL-python
    warning: no files found matching 'MANIFEST'
    warning: no files found matching 'ChangeLog'
    warning: no files found matching 'GPL'
Installing collected packages: MySQL-python
  Running setup.py install for MySQL-python
    building '_mysql' extension
    gcc-4.2 -fno-strict-aliasing -fno-common -dynamic -DNDEBUG -g -fwrapv -Os -Wall -Wstrict-prototypes -DENABLE_DTRACE -pipe -Dversion_info=(1,2,3,'final',0) -D__version__=1.2.3 -I/usr/local/mysql/include -I/System/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6 -c _mysql.c -o build/temp.macosx-10.6-universal-2.6/_mysql.o -Os -g -fno-common -fno-strict-aliasing -arch x86_64
    In file included from _mysql.c:36:
    /usr/local/mysql/include/my_config.h:325:1: warning: "SIZEOF_SIZE_T" redefined
    In file included from /System/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6/Python.h:9,
                     from pymemcompat.h:10,
                     from _mysql.c:29:
    /System/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6/pymacconfig.h:33:1: warning: this is the location of the previous definition
    In file included from _mysql.c:36:
    /usr/local/mysql/include/my_config.h:419:1: warning: "HAVE_WCSCOLL" redefined
    In file included from /System/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6/Python.h:8,
                     from pymemcompat.h:10,
                     from _mysql.c:29:
    /System/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6/pyconfig.h:803:1: warning: this is the location of the previous definition
    gcc-4.2 -Wl,-F. -bundle -undefined dynamic_lookup build/temp.macosx-10.6-universal-2.6/_mysql.o -L/usr/local/mysql/lib -lmysqlclient_r -lpthread -o build/lib.macosx-10.6-universal-2.6/_mysql.so -arch x86_64
    warning: no files found matching 'MANIFEST'
    warning: no files found matching 'ChangeLog'
    warning: no files found matching 'GPL'
Successfully installed MySQL-python
Cleaning up...

之后我尝试了:

$ python -c "import MySQLdb"

它在我身上c with:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/Library/Python/2.6/site-packages/MySQLdb/__init__.py", line 19, in <module>
    import _mysql
ImportError: dlopen(/Library/Python/2.6/site-packages/_mysql.so, 2): Library not loaded: libmysqlclient.16.dylib
  Referenced from: /Library/Python/2.6/site-packages/_mysql.so
  Reason: image not found

关于我的问题......

我做错了什么?/我还需要做什么?

谷歌搜索(并在这里搜索)返回了很多结果,使用Ruby获取此错误消息并不太多使用Python。

7 个答案:

答案 0 :(得分:92)

运行DYLD_LIBRARY_PATHpip install之后设置easy_install

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

假设你的MySQL安装在/usr/local/mysql下。

,应该做的工作

答案 1 :(得分:55)

_mysql.so是指libmysqlclient.16.dylib。也就是说,作为Python和MySQL客户端库_mysql.so之间桥梁的共享库是指MySQL客户端库的动态库,并且由于某种原因无法加载该库。

您需要回答的问题:

  • 您系统上的任何地方都有libmysqlclient.16.dylib吗?如果没有,您需要安装 MySQL客户端软件。
  • 如果是,那么DYLD_LIBRARY_PATH设置中包含该库的目录是什么?如果不, 尝试添加它。
  • 如果是这样,您必须确保libmysqlclient.16.dylib文件没有损坏。我的 复制,安装在/opt/local/lib/mysql5/mysql/libmysqlclient.16.dylib,礼貌 MacPorts具有MD5签名c79ee91af08057dfc269ee212915801a,大小为1,462,376字节。你的副本是什么样的?

答案 2 :(得分:41)

在easy_install之后,我创建了一个解决问题的软链接

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

答案 3 :(得分:8)

如果你的MySQL客户端比MySQL-python包更新,它也会出现。就我而言,我的机器上有一个libmysqlclient_r.18.dylib,但不是libmysqlclient_r.16.dylib。正在运行pip search mysql

  

MySQL-python - Python   MySQL的接口安装:1.2.3   最新消息:1.2.3c1

并且正在运行pip install --upgrade MySQL-python解决了我的问题。

答案 4 :(得分:7)

在最新版本的 MySQL 5.7.9 上,MySQL-python不支持,而是使用了PyMySQL库。我还在manage.py(在Django项目中)添加了这些行来模拟MySQL-python的API:

try:
    # load MySQLdb interface emulation
    import pymysql
    pymysql.install_as_MySQLdb()
except ImportError:
    pass

答案 5 :(得分:6)

在我的设置(来自brew,pyenv的mysql 5.7.x)中,我有一个较新的lib文件libmysqlclient.20.dylib。有效的方法是pip uninstall MySQL-pythonpip install MySQL-python

答案 6 :(得分:0)

对于像我这样需要(或已经)同时安装了MySQLdb和PyMySQL的用户(在我的情况下,我需要同时安装它们,因为我使用PyMySQL连接到本地MySQL实例,而MySQLDb用于远程/活动实例):

确保使用正确的URI方案。要访问本地实例:

LOCAL_DATABASE_URI = 'mysql+pymysql://username:password@hostname/dbname'

并进行直播:

REMOTE_DATABASE_URI = 'mysql+mysqldb://username:password@hostname/dbname'

做出这种区分为我解决了这个问题

相关问题