macos mysql-python 安装问题(在 mac os x lion 上)

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

mysql-python installation problems (on mac os x lion)

pythonmysqlmacosinstallation

提问by AJJ

I installed everything successfully, or so I thought:

我成功安装了所有东西,或者我想:

  • MySQL 5.5 for x86_64.
  • Python 2.7, x86_64.
  • mysql-python 1.2.3, x86_64.
  • 适用于 x86_64 的 MySQL 5.5。
  • Python 2.7,x86_64。
  • mysql-python 1.2.3,x86_64。

But when I try:

但是当我尝试:

import MySQLdb

I get:

我得到:

    ImportError: 
dlopen(/Users/aj/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.6-ix86_64.egg-tmp/_mysql.so, 2): 
no suitable image found.  
Did find:   
/Users/aj/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.6-ix86_64.egg-tmp/_mysql.so: mach-o, 
but wrong architecture

What else can I be missing?

我还能缺少什么?

My system is of course 64bit version as well, uname -agives:

我的系统当然也是 64 位版本,uname -a给出:

Darwin ajmacbook.local 11.1.0 Darwin Kernel Version 11.1.0: Tue Jul 26 16:07:11 PDT 2011; root:xnu-1699.22.81~1/RELEASE_X86_64 x86_64

I think I have read most SO answers and Google results on the subject, can't think of anything else to try. Any suggestion would be appreciated.

我想我已经阅读了关于这个主题的大多数 SO 答案和谷歌结果,想不出还有什么可以尝试的。任何建议将不胜感激。

采纳答案by AJJ

With the help of the comment from @birryree I found the problem. I would probably be better off following the procedure suggested by @birryree in his answer but I did try this before and it worked:

在@birryree 的评论的帮助下,我发现了问题。我可能会更好地遵循@birryree 在他的回答中建议的程序,但我之前确实尝试过这个程序并且它有效:

As suggested, I did:

按照建议,我做了:

file /Users/aj/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.6-ix86_64.egg-tmp/_mysql.so

To get: [...]: Mach-O bundle i386So, wrong architecture. From there I did the same with mysql and python just to be sure: file $(which python)gave:

得到:[...]: Mach-O bundle i386所以,错误的架构。从那里我对 mysql 和 python 做了同样的事情,只是为了确定: file $(which python)给:

/Library/Frameworks/Python.framework/Versions/2.7/bin/python: Mach-O universal binary with 2 architectures
/Library/Frameworks/Python.framework/Versions/2.7/bin/python (for architecture i386):   Mach-O executable i386
/Library/Frameworks/Python.framework/Versions/2.7/bin/python (for architecture x86_64): Mach-O 64-bit executable x86_64

And file $(which mysql):

并且file $(which mysql)

/usr/local/mysql/bin/mysql: Mach-O 64-bit executable x86_64

So I uninstalled the mysql-python package: sudo pip uninstall mysql-pythonand installed it again. But doing this I realized my previous mistake while installing this package. First time I typed:

所以我卸载了 mysql-python 包:sudo pip uninstall mysql-python并重新安装了它。但是这样做我在安装这个包时意识到了我以前的错误。我第一次输入:

sudo ARCHFLAGS='-arch ix86_64' python setup.py build(and "install" afterwards)

sudo ARCHFLAGS='-arch ix86_64' python setup.py build(然后“安装”)

The architecture name was wrong, should be '-arch x86_64', no "i", so it just ignored my flag and installed the 32bit.

架构名称错误,应该是'-arch x86_64',没有“i”,所以它只是忽略了我的标志并安装了32位。

The proper commands to install the downloaded mysql-python package for 64bit (from the source folder):

安装下载的 64 位 mysql-python 包的正确命令(来自源文件夹):

sudo ARCHFLAGS='-arch x86_64' python setup.py build
sudo ARCHFLAGS='-arch x86_64' python setup.py install

回答by wkl

I think there might be slight quirks with doing this on Mac 64-bit (and if you google this problem shows up a lot too).

我认为在 Mac 64 位上执行此操作可能会有一些小怪癖(如果你用谷歌搜索这个问题也会出现很多)。

I've run into it, and there are a couple things you can do:

我遇到了它,你可以做几件事:

Override the environment

覆盖环境

You can change the DYLD_LIBRARY_PATHenvironment variable, which tells the linker where to look for dynamic libraries (.so files and such). You said you also downloaded the 64-bit version of MySQL, so where ever it's installed, change the path you see here:

您可以更改DYLD_LIBRARY_PATH环境变量,它告诉链接器在哪里查找动态库(.so 文件等)。您说您还下载了 64 位版本的 MySQL,因此无论安装在哪里,请更改您在此处看到的路径:

In a shell:

在外壳中:

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

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

And then run pythonand see if you can import MySQLdb.

然后跑python,看看能不能import MySQLdb

If that works, you can make this permanent by altering your shell profile (.bash_profile, most likely).

如果可行,您可以通过更改您的 shell 配置文件(.bash_profile最有可能的)来使其永久化。

Use homebrew

使用自制软件

I don't really like mucking around with making sure MySQL and Python and all that are correct architectures and installing them separately. I run homebrew, which is a sort of package manager for Mac. If you install that, you can pretty easily take care of this issue:

我真的不喜欢确保 MySQL 和 Python 以及所有正确的架构并分别安装它们。我运行homebrew,这是一种适用于 Mac 的包管理器。如果你安装它,你可以很容易地解决这个问题:

  • brew install python
  • brew install mysql
  • /usr/local/share/python/easy_install mysql-python
  • brew install python
  • brew install mysql
  • /usr/local/share/python/easy_install mysql-python

Do note that homebrew installs into /usr/local, so you should add /usr/local/binto your PATH, ahead of /usr/binand /bin, otherwise you'll get really confused why pythonis different.

做注意到自制软件安装到/usr/local,所以你应该添加/usr/local/bin到您的PATH,超前的/usr/bin/bin,否则你会得到真正的困惑,为什么python是不同的。

You can add /usr/local/share/pythonto your PATHas well, to make it permanent.

您也可以添加/usr/local/share/python到您的PATH,以使其永久化。

回答by chizzle

VERY IMPORTANT!

很重要!

As mentioned above, please make sure you are running the 64-bitversion of mysql. It's easy to overlook this detail especially if you've upgraded from Snow Leopard. (I certainly did).

如上所述,请确保您运行的是64 位版本的 mysql。很容易忽略这个细节,尤其是如果您是从 Snow Leopard 升级的。(我当然有)。

if you're not sure about removing the older version of mysql on your system, refer to this post: http://johnmcostaiii.net/2011/removing-mysql-osx-lion/

如果您不确定是否删除系统上旧版本的 mysql,请参阅此帖子:http: //johnmcostaiii.net/2011/removing-mysql-osx-lion/

回答by Yassine ElBadaoui

I had the same problem, and a lot of headache with MySQLdb after fixing the 64bit issue (it was complaining also about where is libmysqlclient.18.dylib).

在修复 64 位问题后,我遇到了同样的问题,并且对 MySQLdb 感到很头疼(它还在抱怨 libmysqlclient.18.dylib 在哪里)。

I think it's time to switch to the official MysQL Python Connector?

我认为是时候切换到官方的 MySQL Python 连接器了?

sudo pip install mysql-connector-python

Or download it from: http://dev.mysql.com/downloads/connector/python/

或从以下网址下载:http: //dev.mysql.com/downloads/connector/python/

Documentation: http://dev.mysql.com/doc/refman/5.5/en/connector-python.htm

文档:http: //dev.mysql.com/doc/refman/5.5/en/connector-python.htm

It's easy to use and also compatible with PEP 249 (Python DB API version 2.0).

它易于使用且与 PEP 249(Python DB API 2.0 版)兼容。

回答by Adam Spence

Also make sure you are running Python 64-bittoo. I was running mysql 64 bit and Python 32bit so got the 'but wrong architecture' error

还要确保您也在运行Python 64 位。我正在运行 64 位 mysql 和 32 位 Python,因此出现“但架构错误”错误