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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4559699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 16:18:59  来源:igfitidea点击:

Python: MySQLdb and "Library not loaded: libmysqlclient.16.dylib"

pythonmysqlmacososx-snow-leopard

提问by rennat

The setup...

设置...

Trying to set up a clean Mac os X 10.6 install to develop python/django and I didn't remember running into this on 10.5.

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

After installing MySQL from the installer on mysql-5.5.8-osx10.6-x86_64.dmgI ran

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

$ sudo pip install MySQL-python

and it seemed to go smoothly (output below)

它似乎进行得很顺利(下面的输出)

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...

after this I tried:

在这之后我试过:

$ python -c "import MySQLdb"

and it crapped out on me 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

So on to my question...

那么继续我的问题...

What did I do wrong?/What else do I need to do?

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

Googling (and searching here) for this returns a lot of results getting this error message with Ruby not too many with Python tho.

谷歌搜索(并在此处搜索)会返回很多结果,使用 Ruby 获得此错误消息,而使用 Python 则不会太多。

采纳答案by Brian Clapper

_mysql.sorefers to libmysqlclient.16.dylib. That is, the shared library that serves as the bridge between Python and the MySQL client library, _mysql.so, refers to the dynamic library for the MySQL client library, and that library cannot be loaded for some reason.

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

Questions you need to answer:

您需要回答的问题:

  • Is there a libmysqlclient.16.dylibanywhere on your system? If not, you need to install the MySQL client software.
  • If so, is the directory containing that library in your DYLD_LIBRARY_PATHsetting? If not, try adding it.
  • If so, you'll have to ensure that the libmysqlclient.16.dylibfile is not corrupt. My copy, installed in /opt/local/lib/mysql5/mysql/libmysqlclient.16.dylib, courtesy of MacPorts, has MD5 signature c79ee91af08057dfc269ee212915801aand is 1,462,376 bytes in size. What does your copy look like?
  • libmysqlclient.16.dylib你的系统上有任何地方吗?如果没有,则需要安装 MySQL 客户端软件。
  • 如果是这样,您的DYLD_LIBRARY_PATH设置中是否包含该库的目录?如果没有,请尝试添加它。
  • 如果是这样,您必须确保libmysqlclient.16.dylib文件没有损坏。我的副本安装在/opt/local/lib/mysql5/mysql/libmysqlclient.16.dylib,由 MacPorts 提供,具有 MD5 签名c79ee91af08057dfc269ee212915801a,大小为 1,462,376 字节。你的副本是什么样的?

回答by lukmdo

Just set the DYLD_LIBRARY_PATHafter running pip installor easy_install:

只需设置DYLD_LIBRARY_PATH后运行pip installeasy_install

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

Should do the job assuming your MySQL installation lives under /usr/local/mysql.

假设您的 MySQL 安装位于/usr/local/mysql.

回答by Catherine Devlin

It can also crop up if your MySQL client is newer than your MySQL-python package. In my case, I had a libmysqlclient_r.18.dylib on my machine, but not a libmysqlclient_r.16.dylib. Running pip search mysqlrevealed

如果您的 MySQL 客户端比您的 MySQL-python 包更新,它也会出现。就我而言,我的机器上有一个 libmysqlclient_r.18.dylib,但没有 libmysqlclient_r.16.dylib。跑步pip search mysql揭秘

MySQL-python - Python interface to MySQL INSTALLED: 1.2.3 LATEST: 1.2.3c1

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

and running pip install --upgrade MySQL-pythonfixed my problem.

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

回答by msbanik

After easy_install, I create a soft link that solved the problem

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

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

回答by Anton Danilchenko

On the latest version of MySQL 5.7.9it's no support from MySQL-pythonand I used the PyMySQLlibrary instead. Also I added in manage.py(in Django project) these lines to emulate API of MySQL-python:

在最新版本的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

回答by Giannis

On my setup (mysql 5.7.x from brew, pyenv), I had a newer lib file libmysqlclient.20.dylib. What worked was to pip uninstall MySQL-pythonand pip install MySQL-python.

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

回答by kip2

For those like me who need - or have - both MySQLdb & PyMySQL installed (in my case, I needed to have both installed because I use PyMySQL to connect to my local MySQL instances, and MySQLDb for remote/live instances):

对于像我这样需要 - 或已经 - 安装 MySQLdb 和 PyMySQL 的人(在我的情况下,我需要安装两个,因为我使用 PyMySQL 连接到我的本地 MySQL 实例,而 MySQLDb 用于远程/实时实例):

Be sure you're using the right URI scheme. To access the local instances:

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

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

and for live:

和现场:

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

Making this distinction solved the issue for me

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