下載MySQLdb
在SourceForge可以下載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安裝MySQLdb
在終端中執行:
new-host-3:~ iFantastic$ pip install MySQL-python
無論是在線安裝還是下載安裝,此時你可能會遇到第一個錯誤提示:
EnvironmentError: mysql_config not found
建議使用pip安裝,因爲我從sourceforge下載的版本居然不如pip中的新。如果想要升級mysql-python版本,可以使用以下命令:
$ easy_install MySQL-python --upgrade
[注意]如果你沒有按照下文解決安裝中的錯誤提示,那麼升級也會因爲同樣的錯誤原因而失敗。
解決mysql_config not found錯誤
在默認方式安裝Mac版MySql時,會在/usr/local/mysql/bin目錄下生成mysql_config文件。
因此下載安裝時的解決辦法爲:在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的錯誤。
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進行選擇。
另外再提供一個pip安裝時找不到mysql_config的解決方法(我沒有驗證),在終端中輸入以下命令:
$ sudo ln -s /usr/local/mysql/bin/* /usr/bin
好了,到這裏,MySQL-python包應該基本順利安裝。
解決 Reason: image not found 錯誤
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: unknown argument: '-mno-fused-madd' [-Wunused-command-line-argument-hard-error-in-future]
clang: note: this will be a hard error (cannot be downgraded to a warning) in the future
再用pip下載一遍,此時如果還有報錯,可加上–user。
例如:
pip install MySQL-python --user