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.
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/_m开发者_如何学JAVAysql.so: mach-o,
but wrong architecture
What else can I be missing?
My system is of course 64bit version as well, uname -a
gives:
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.
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).
I've run into it, and there are a couple things you can do:
Override the environment
You can change the DYLD_LIBRARY_PATH
environment 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:
In a shell:
export DYLD_LIBRARY_PATH=/usr/local/mysql/lib/
And then run python
and see if you can import MySQLdb
.
If that works, you can make this permanent by altering your shell profile (.bash_profile
, most likely).
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:
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/bin
to your PATH
, ahead of /usr/bin
and /bin
, otherwise you'll get really confused why python
is different.
You can add /usr/local/share/python
to your PATH
as well, to make it permanent.
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:
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 i386
So, wrong architecture. From there I did the same with mysql and python just to be sure:
file $(which python)
gave:
/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)
:
/usr/local/mysql/bin/mysql: Mach-O 64-bit executable x86_64
So I uninstalled the mysql-python package: sudo pip uninstall mysql-python
and installed it again. But doing this I realized my previous mistake while installing this package. First time I typed:
sudo ARCHFLAGS='-arch ix86_64' python setup.py build
(and "install" afterwards)
The architecture name was wrong, should be '-arch x86_64', no "i", so it just ignored my flag and installed the 32bit.
The proper commands to install the downloaded mysql-python package for 64bit (from the source folder):
sudo ARCHFLAGS='-arch x86_64' python setup.py build
sudo ARCHFLAGS='-arch x86_64' python setup.py install
VERY IMPORTANT!
As mentioned above, please make sure you are running the 64-bit version of mysql. It's easy to overlook this detail especially if you've upgraded from Snow Leopard. (I certainly did).
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/
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).
I think it's time to switch to the official MysQL Python Connector?
sudo pip install mysql-connector-python
Or download it from: http://dev.mysql.com/downloads/connector/python/
Documentation: 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).
Also make sure you are running Python 64-bit too. I was running mysql 64 bit and Python 32bit so got the 'but wrong architecture' error
精彩评论