轉載:http://blog.csdn.net/janronehoo/article/details/25207825html
短評:這篇文章感受是比較全面解決Mac MySQLdb模塊安裝問題的文章了,特別轉載一下python
安裝過程主要是mysql_config not found錯誤,這個錯誤一般卡住不少初學者,以及安裝後出現的 image not found 錯誤mysql
版本:Python 2.7.3sql
MySQL-python包中,所以不管下載仍是在pip中search,都應該是搜尋MySQL-python。vim
下載MySQLdbbash
MySQL-python-1.2.4b4.tar,下載後解壓,而後在終端Terminal中執行如下命令:編輯器
new-host-3:~ iFantastic$ cd /Users/iFantastic/Downloads/MySQL-python-1.2.4b4 new-host-3:MySQL-python-1.2.4b4 iFantastic$ python setup.py install
使用PIP安裝MySQLdbui
new-host-3:~ iFantastic$ pip install MySQL-python
不管是在線安裝仍是下載安裝,此時你可能會遇到第一個錯誤提示:this
EnvironmentError: mysql_config not found
解決mysql_config not found錯誤spa
所以下載安裝時的解決辦法爲:在MySQL-python的安裝包中找到site.cfg文件,打開它,找到如下內容:
# The path to mysql_config. # Only use this if mysql_config is not on your PATH, or you have some weird # setup that requires it. # mysql_config = /usr/local/bin/mysql_config
將最後一句句首井號去掉,並修改成:
mysql_config = /usr/local/mysql/bin/mysql_config
而後執行
$ python setup.py install
通常說來,此時安裝能夠完成,但仍有問題,下文會繼續闡述。
使用pip安裝時沒有辦法修改site.cfg文件,所以能夠經過修改OS X的系統環境變量來解決找不到mysql_config的錯誤。
修改OS X環境變量:打開終端,在終端中使用vim打開「~/.bash_profile」,若是沒有安裝vim,那就顯示隱藏文件用文本編輯器打開,具體操做這裏就不復述了。在.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
其中 VERSIONER_PYTHON_PREFER_64_BIT和VERSIONER_PYTHON_PREFER_64_BIT根據本身安裝的MySQL進行選擇。
$ sudo ln -s /usr/local/mysql/bin/* /usr/bin
解決 Reason: image not found 錯誤
安裝完MySQL-python包後,讓咱們import MySQLdb,此時出現一個錯誤,錯誤最後一行寫着 Reason: image not found。
解決方法是在終端執行:
$ 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
錯誤:
clang: error: clang: note: this will be a hard error (cannot be downgraded to a warning) in the future
經網上查證:http://www.tuicool.com/articles/zI7Vzu貌似是mac os的Xcode從5.1起給編譯器規定對於未知參數傳入視爲error咱們須要使用ARCHFLAGS將該error降級爲warning所以最後的安裝命令應該以下:
sudo ARCHFLAGS=-Wno-error=unused-command-line-argument-hard-error-in-future python setup.py build
參考:
http://www.cnblogs.com/macro-cheng/archive/2011/10/25/mysql-001.html