简体   繁体   中英

Installing MySQLdb on Mac OS X

I've spent several hours trying to install MySQLdb (the Python library) on Mac OS X Snow Leopard. I'm using these instructions from SO. I keep getting an error, so I've tried using MacPorts (as one of the answers to that question advises), but I continue to get the same error. Can anybody help?

import MySQLdb
Traceback (most recent call last):
  File "<stdin>", 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/josephmornin/.python-egg-cache/MySQL_python-1.2.3-py2.6-macosx-10.6-universal.egg-tmp/_mysql.so, 2): no suitable image found.  
  Did find:
    /Users/josephmornin/.python-egg-cache/MySQL_python-1.2.3-py2.6-macosx-10.6-universal.egg-tmp/_mysql.so: mach-o, but wrong architecture

sudo pip install mysql-python在 10.8.1 中为我工作。

Ack. I feel your pain. I spent a really long time also trying to get MySQL working with Python 2.6 on Snow Leopard using a Macbook Air and had lots of architecture problems. What ended up solving it for me, was making sure both my Python and MySQL installations were using a 32 bit architecture like my Snow Leopard was.

I wrote about my solution here, so maybe that'll help:

http://www.markliu.me/2010/jun/09/mysql-and-python-on-32-bit-snow-leopard/

Good luck...

try this in .bashrc or .bash_profile

PATH="/usr/local/mysql/bin:${PATH}"
export PATH
export DYLD_LIBRARY_PATH=/usr/local/mysql/lib/
export VERSIONER_PYTHON_PREFER_64_BIT=no
export VERSIONER_PYTHON_PREFER_32_BIT=yes

您还可以强制 python 在 32 位模式下运行:

defaults write com.apple.versioner.python Prefer-32-Bit -bool yes

This answer is an update, circa November 2019. None of the popular pip installs will give you a working setup on MacOS 10.13 (and likely other versions as well). Here is a simple way that I got things working:

brew install mysql
pip install mysqlclient

If you need help installing brew, see this site: https://brew.sh/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM