Python初學(xué)赏参,要連接到數(shù)據(jù)庫MySQL,需要安裝接口程序: MySQL-Python (即:MySQLdb模塊)
因用的Mac電腦沿盅,安裝過程頗費(fèi)一番波折把篓,在Google大神的輔導(dǎo)下終于搞定,特此記錄以總結(jié)經(jīng)驗(yàn):
環(huán)境說明:
系統(tǒng):MacOS 10.11
Python:系統(tǒng)自帶的2.7.10
-
第1步:下載MySQL-Python腰涧,并解壓
下載地址:從sourceforge下載
插曲說明:
sourceforge站點(diǎn)牛逼的做了系統(tǒng)識(shí)別韧掩,
如果是Mac系統(tǒng)則下載的包為.tar的壓縮包,下載后解壓即可窖铡;
如果是Win系統(tǒng)則下載的是exe的安裝包疗锐;
慚愧,在這點(diǎn)上吃了大虧费彼,當(dāng)時(shí)不知道是系統(tǒng)哪里出了錯(cuò)滑臊,硬是只給我下載exe包,折騰了半天箍铲,最后還是到GitHub上MySQLdb源下載了源代碼包來安裝的
第2步:修改site.cfg文件
解壓MySQL-Python后雇卷,按理應(yīng)該直接運(yùn)行安裝,可是如果直接用第3步的安裝,會(huì)出現(xiàn)mysql_config not found錯(cuò)誤关划,要解決這個(gè)錯(cuò)誤小染,在MySQL-python的安裝包中找到site.cfg文件,打開它贮折,找到以下內(nèi)容:
# The path to mysql_config.
# Only use this if mysql_config is not on yourPATH, or you have some weird
# setup that requires it.
# mysql_config = /usr/local/bin/mysql_config
上文最后一句代碼指示的是mysql_config
的地址裤翩,默認(rèn)是屏蔽的狀態(tài);
去掉這一句前面的#
调榄,并將mysql_config
地址改為/usr/local/mysql/bin/mysql_config
岛都,因?yàn)檫@是mysql_config
默認(rèn)的真實(shí)地址(mac安裝mysql默認(rèn)這個(gè)地址下有mysql_config
文件,想確認(rèn)的朋友可以Shift+Command+G
前往這個(gè)目錄查看)振峻,代碼改后如下:
mysql_config = /usr/local/mysql/bin/mysql_config第2步:安裝python
運(yùn)行終端(Terminal)程序臼疫,在窗口中執(zhí)行:
$ python setup.py install
運(yùn)行安裝程序并提示成功后,MySQL-Python就安裝成功了扣孟,這個(gè)時(shí)候可在終端窗口驗(yàn)證:
$ Python
$ import MySQLdb
若沒有提示錯(cuò)誤烫堤,即安裝成功!-
報(bào)錯(cuò)說明:網(wǎng)上教程里說會(huì)遇到 Reason: image not found 的錯(cuò)誤凤价,(如果你遇到可以百度下解決方案)鸽斟,我沒有遇到這個(gè)報(bào)錯(cuò),不過卻遇到另一個(gè)報(bào)錯(cuò):
error: can't create or remove files in install directory
The following error occurred while trying to add or remove files in theinstallation directory:
[Errno 13] Permission denied: '/Library/Python/2.7/site-packages/test-easy-install-3121.write-test'
The installation directory you specified (via --install-dir, --prefix, orthe distutils default setting) was:
/Library/Python/2.7/site-packages/
Perhaps your account does not have write access to this directory? If theinstallation directory is a
system-owned directory, you may need to sign inas the administrator or "root" account. If you do not
have administrativeaccess to this machine, you may wish to choose a different installationdirectory,
preferably one that is listed in your PYTHONPATH environmentvariable.
For information on other options, you may wish to consult thedocumentation at:
http://packages.python.org/distribute/easy_install.html
Please make the appropriate changes for your system and try again.
經(jīng)過百度后利诺,終于知道原因富蓄,將命令改為:sudo python setup.py install
然后輸入密碼,這樣就安裝成功了慢逾!
參考來源:Strack overflow: Python - [Errno 13] Permission denied
額外收獲:知道了Python和MySQL的安裝地址
Python:/usr/bin/python
MySQL:/usr/local/mysql/
想確認(rèn)的朋友可以Shift+Command+G
前往這個(gè)目錄查看
.
小總結(jié):
搞產(chǎn)品這么久立倍,職業(yè)慣性告訴我這種安裝體驗(yàn)糟糕透了,正常安裝途徑完全受阻侣滩,原因我還沒搞透徹口注,不得不百度解決!很簡單的過程君珠,希望Python后續(xù)這種表現(xiàn)少一些寝志。