macos Python 导入 MySQLdb 错误 - Mac 10.6

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4730787/
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-10-21 07:37:40  来源:igfitidea点击:

Python import MySQLdb error - Mac 10.6

pythonmysqlmacos

提问by yanigisawa

I downloaded and followed the install instructions for MySQL 5.5.8 (http://dev.mysql.com/downloads/mysql/) and for the MySQLdb python plugin. (http://sourceforge.net/projects/mysql-python/)

我下载并遵循 MySQL 5.5.8 ( http://dev.mysql.com/downloads/mysql/) 和 MySQLdb python 插件的安装说明。( http://sourceforge.net/projects/mysql-python/)

When I attempt to import MySQLdb to a python terminal, I get the below error:

当我尝试将 MySQLdb 导入 python 终端时,出现以下错误:

Safira:~ yanigisawa$ python --version
Python 2.6.1
Safira:~ yanigisawa$ python -c "import MySQLdb"
Traceback (most recent call last):?
  File "<string>", line 1, in <module>
  File "build/bdist.macosx-10.6-universal/egg/MySQLdb/__init__.py", line 19, in <module>
  File "build/bdist.macosx-10.6-universal/egg/_mysql.py", line 7, in <module>
  File "build/bdist.macosx-10.6-universal/egg/_mysql.py", line 6, in __bootstrap__
ImportError: dlopen(/Users/yanigisawa/.python-eggs/MySQL_python-1.2.3-py2.6-macosx-10.6-universal.egg-tmp/_mysql.so, 2): Library not loaded: libmysqlclient.16.dylib
  Referenced from: /Users/yanigisawa/.python-eggs/MySQL_python-1.2.3-py2.6-macosx-10.6-universal.egg-tmp/_mysql.so
  Reason: image not found
Safira:~ yanigisawa$ 

I found question 4559699on stack overflow, and followed the instructions there:

我在堆栈溢出时发现了问题4559699,并按照那里的说明进行操作:

Safira:~ yanigisawa$ echo $DYLD_LIBRARY_PATH
/usr/local/mysql/lib
Safira:~ yanigisawa$ ls -l /usr/local/mysql/lib
total 363224
-rwxr-xr-x   1 root  wheel   3787328 Jan 18 19:41 libmysqlclient.16.dylib
-rw-r--r--   1 root  wheel  10035336 Dec  3 13:26 libmysqlclient.a
lrwxr-xr-x   1 root  wheel        23 Jan 14 22:01 libmysqlclient.dylib -> libmysqlclient.16.dylib
lrwxr-xr-x   1 root  wheel        20 Jan 14 22:01 libmysqlclient_r.16.dylib -> libmysqlclient.dylib
lrwxr-xr-x   1 root  wheel        16 Jan 14 22:01 libmysqlclient_r.a -> libmysqlclient.a
lrwxr-xr-x   1 root  wheel        20 Jan 14 22:01 libmysqlclient_r.dylib -> libmysqlclient.dylib
-rw-r--r--   1 root  wheel  88033760 Dec  3 13:22 libmysqld-debug.a
-rw-r--r--   1 root  wheel  84075304 Dec  3 13:26 libmysqld.a
-rw-r--r--   1 root  wheel      8472 Dec  3 13:26 libmysqlservices.a
drwxr-xr-x  13 root  wheel       442 Dec  3 13:53 plugin
Safira:~ yanigisawa$ md5 /usr/local/mysql/lib/libmysqlclient.16.dylib 
MD5 (/usr/local/mysql/lib/libmysqlclient.16.dylib) = b8a78a93183720021b9dbe9ddad57111
Safira:~ yanigisawa$ 

And after cleaning all of the python egg files, and rebuilding, I still got the same error. Then I found bug 59006 (http://bugs.mysql.com/bug.php?id=59006) in MySQL 5.5.8 affecting Mac 10.6. I tried the work-around listed there, cleaned eggs and rebuilt, and I'm still getting this error.

在清理了所有的 python egg 文件并重建后,我仍然遇到同样的错误。然后我在 MySQL 5.5.8 中发现了影响 Mac 10.6 的错误 59006 ( http://bugs.mysql.com/bug.php?id=59006)。我尝试了那里列出的解决方法,清理鸡蛋并重建,但我仍然收到此错误。

Is there another work-around for this problem specific to python? Am I missing something else?

对于这个特定于 python 的问题,还有其他解决方法吗?我还缺少其他东西吗?

EDIT: Adding output from otool commands:

编辑:从 otool 命令添加输出:

Safira:~ yanigisawa$ otool -L /Users/yanigisawa/.python-eggs/MySQL_python-1.2.3-py2.6-macosx-10.6-universal.egg-tmp/_mysql.so
/Users/yanigisawa/.python-eggs/MySQL_python-1.2.3-py2.6-macosx-10.6-universal.egg-tmp/_mysql.so:
    libmysqlclient.16.dylib (compatibility version 16.0.0, current version 16.0.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 125.2.1)
Safira:~ yanigisawa$ otool -DX /usr/local/mysql/lib/libmysqlclient.16.dylib
/usr/local/mysql/lib/libmysqlclient.16.dylib
Safira:~ yanigisawa$ 

回答by Chris

In my case, I solved by adding a couple of symbolic links as in http://ageekstory.blogspot.com/2011/04/installing-massive-coupon-on-mac-os-10.html

就我而言,我通过添加几个符号链接来解决,如http://ageekstory.blogspot.com/2011/04/installing-massive-coupon-on-mac-os-10.html

as following:

如下:

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

sudo ln -s /usr/local/mysql/lib /usr/local/mysql/lib/mysql

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

须藤 ln -s /usr/local/mysql/lib /usr/local/mysql/lib/mysql

回答by Ned Deily

You could try running otool to find out exactly what library paths the MySQLdbC extension, _mysql.sois looking for:

您可以尝试运行 otool 以准确找出MySQLdbC 扩展_mysql.so正在寻找的库路径:

$ otool -L /Users/yanigisawa/.python-eggs/MySQL_python-1.2.3-py2.6-macosx-10.6-universal.egg-tmp/_mysql.so

and then the installed library name of the MySQL library file:

然后是MySQL库文件的已安装库名称:

$ otool -DX /usr/local/mysql/lib/libmysqlclient.16.dylib

But you should not have to resort to setting DYLD_LIBRARY_PATH(or using install_name_toolto modify an executable); the need to set it is almost always a sign of a component that was built or installed incorrectly. In my experience, the MySQL project does not have a very good track record for consistency in their OS X builds and installers, as that issue you cite tends to confirm.

但是您不必求助于设置DYLD_LIBRARY_PATH(或install_name_tool用于修改可执行文件);需要设置它几乎总是一个组件构建或安装不正确的标志。根据我的经验,MySQL 项目在其 OS X 构建和安装程序的一致性方面没有很好的跟踪记录,因为您引用的那个问题往往会证实。

Trying to get a working combination of Python, MySQLdb, and MySQLlibraries in OS X 10.6 is a very common cause of frustration: lots of questions about it here on SO and elsewhere. There are a number reasons for that. My advice is to go with a complete solution from one of the third-party distributors that will build and install compatible versions of everything you need. And it should keep working if you need to upgrade components as time goes on. I've had good success over the years with MacPorts; others prefer Finkor the newer HomeBrew. With MacPorts, after installing the base files from the MacPorts .dmg, you can get everything built like so:

试图让的工作相结合PythonMySQLdb以及MySQL在OS X 10.6库是无奈的一个非常常见的原因:很多关于它的问题在这里SO和其他地方。这有很多原因。我的建议是使用来自第三方分销商之一的完整解决方案,该分销商将构建和安装您需要的所有内容的兼容版本。如果您需要随着时间的推移升级组件,它应该继续工作。多年来,我取得了很好的成功MacPorts;其他人更喜欢Fink或更新HomeBrew。随着MacPorts,从 MacPorts 安装基本文件后。dmg,您可以像这样构建所有内容:

$ sudo port selfupdate  # ensure the port files are up-to-date
$ sudo port install py26-mysql

The MacPorts Python will be available at /opt/local/bin/python2.6. If you are looking to run Django, there's a port for that as well.

MacPorts Python 将在/opt/local/bin/python2.6. 如果您想运行Django,也有一个端口。

EDIT: With the updated output from otool, you can see that there is a mismatch of library path names. The MySQLdb extension is asking for a relative path name (no initial /), while the MySQL library advertises itself with an absolute path, normally what you want. It seems to be the reverse of the MySQL issue 59006 but, without knowing exactly what steps you've performed up until now, it is really hard to say how you got to this state. You maybe able to use install_name_toolto patch the _mysql.soto have the absolute path name which would also eliminate any need to set DYLD_LIBRARY_PATH. It's worth trying:

编辑:使用来自 的更新输出otool,您可以看到库路径名称不匹配。MySQLdb 扩展要求一个相对路径名(无首字母/),而 MySQL 库用绝对路径宣传自己,通常是你想要的。这似乎是 MySQL 问题 59006 的反面,但是,在不确切知道您到目前为止执行了哪些步骤的情况下,很难说您是如何达到这种状态的。您可以使用install_name_tool修补程序_mysql.so以具有绝对路径名,这也将消除任何设置DYLD_LIBRARY_PATH. 值得一试:

$ cd ~/.python-eggs/MySQL_python-1.2.3-py2.6-macosx-10.6-universal.egg-tmp
$ sudo install_name_tool -change libmysqlclient.16.dylib \
                                 /usr/local/mysql/lib/libmysqlclient.16.dylib \
                         _mysql.so

But that should not leave you with a warm and fuzzy feeling that you can reproduce the results when you need to update something. There really is no virtue in trying to glue all these pieces from different sources into something that works.

但这不应该让您有一种温暖和模糊的感觉,即在需要更新某些内容时可以重现结果。试图将来自不同来源的所有这些片段粘合成有效的东西确实没有任何好处。

回答by Kevin

I tried addressing the problem of an unfound dynamics library by compiling the MySQLdb module with static libraries (set static = Truein site.cfg). But that generated the same error, as _mysql.o still was asking for the dynamic library. To fix that, I added libraries = []to the block for if static in setup_posix.py.

我尝试通过使用静态库(set static = True在 site.cfg 中)编译 MySQLdb 模块来解决未找到的动态库的问题。但这产生了同样的错误,因为 _mysql.o 仍然要求动态库。为了解决这个问题,我libraries = []在 setup_posix.py 中添加了 if static 块。