前面
Python寫的項(xiàng)目需要連接MySQL數(shù)據(jù)庫,所以本地需要安裝MySQLdb模塊,但是在安裝的過程中也是遇到了一些問題,在這里記錄一下,不得不說,簡書的Markdown編輯器用著太爽了,用了很久,還是喜歡這種簡潔簡單的東西,讓你更關(guān)注內(nèi)容,而不是其它。
操作系統(tǒng):MacOS 10.12.5
Python版本:2.7.10
pip: 9.0.1
開始吧
一般的,如果你使用了連接MySQL的代碼,或者依賴于MySQLdb類庫的類庫,就會看到這個(gè):ImportError: No module named MySQLdb,提示找不到模塊。
這個(gè)時(shí)候我們就需要安裝對應(yīng)的MySQL for Python了,也就是MySQL-python,在終端執(zhí)行pip install MySQL-python,額~前提是你已經(jīng)安裝pip了啊。
遇到錯誤
當(dāng)你按下回車鍵的那一剎那,屏幕上,就出現(xiàn)了錯誤:EnvironmentError: mysql_config not found,嗯,這是咋回事,網(wǎng)上查查去。。。
粗略看了一下,大概意思就是,你還需要安裝一個(gè)MySQL,What?我就是想連接一下數(shù)據(jù)庫,不至于還要下一個(gè)數(shù)據(jù)庫吧。仔細(xì)看了看又,原來是需要安裝一個(gè)mysql開發(fā)包(當(dāng)然你也可以本地安裝一個(gè)MySQL),好吧,沒辦法,那再來。

手動下載了dmg版本的,根據(jù)提示安裝完畢,終端運(yùn)行pip install MySQL-python發(fā)下還是原來的錯誤,又去查了查資料,應(yīng)該是安裝的mysql庫沒有加入環(huán)境變量,現(xiàn)在就是要找到我剛才安裝的mysql到哪個(gè)目錄了。終端窗口執(zhí)行find / -name mysql_config,結(jié)果中有一個(gè):/usr/local/mysql-connector-c-6.1.10-macos10.12-x86_64/bin/mysql_config
,應(yīng)該就是它了,通過:export PATH=$PATH:/usr/local/mysql-connector-c-6.1.10-macos10.12-x86_64/bin/加入環(huán)境變量。
再次錯誤
再次執(zhí)行pip install MySQL-python,看到輸出的錯誤,我簡直崩潰了。
Collecting MySQL-python
Using cached MySQL-python-1.2.5.zip
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/private/var/folders/d1/1lnf846x6p119488dvhnqj740000gn/T/pip-build-b16vZo/MySQL-python/setup.py", line 17, in <module>
metadata, options = get_config()
File "setup_posix.py", line 53, in get_config
libraries = [ dequote(i[2:]) for i in libs if i.startswith(compiler_flag("l")) ]
File "setup_posix.py", line 8, in dequote
if s[0] in "\"'" and s[0] == s[-1]:
IndexError: string index out of range
----------------------------------------
換一種方式
還能不能愉快的玩耍了~
Google了一下,沒有解決,我想我要換一種方式了。
把剛才安裝的mysql開發(fā)包刪掉。
通過brew重新安裝試試。
# install brew
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
有興趣的同學(xué)可以去brew官網(wǎng)看看,居然有中文,厚道:https://brew.sh/index_zh-cn.html
懷疑人生/勝利在望
這一次直接brew install mysql,等待安裝完畢,再再再執(zhí)行pip install MySQL-python,嗯~果然還是錯誤,這就是人生啊。
3 warnings generated.
cc -bundle -undefined dynamic_lookup -arch x86_64 -arch i386 -Wl,-F. build/temp.macosx-10.12-intel-2.7/_mysql.o -L/usr/local/Cellar/mysql/5.7.18_1/lib -lmysqlclient -lssl -lcrypto -o build/lib.macosx-10.12-intel-2.7/_mysql.so
ld: library not found for -lssl
clang: error: linker command failed with exit code 1 (use -v to see invocation)
error: command 'cc' failed with exit status 1
就是上面這個(gè)錯誤,于是乎,我又Google,發(fā)現(xiàn)了:https://stackoverflow.com/questions/22697440/cc-failed-with-exit-status-1-error-when-install-python-library

然后我通過大神的指示:xcode-select --install,會彈出xcode的軟件安裝窗口,等待幾分鐘,安裝完畢,再次執(zhí)行:pip install MySQL-python。
呼~成功了你相信嗎?
Collecting MySQL-python
Downloading MySQL-python-1.2.5.zip (108kB)
100% |████████████████████████████████| 112kB 127kB/s
Installing collected packages: MySQL-python
Running setup.py install for MySQL-python ... done
Successfully installed MySQL-python-1.2.5
終于見到你,還好我沒放棄??
程序員就是要解決他遇到的一個(gè)又一個(gè)的問題。