1. 程式人生 > >mac裝python包mysql_python遇到的問題

mac裝python包mysql_python遇到的問題

Python初學,要連線到資料庫MySQL,需要安裝介面程式: MySQL-Python (即:MySQLdb模組)

因用的Mac電腦,安裝過程頗費一番波折,在Google大神的輔導下終於搞定,特此記錄以總結經驗:

環境說明
系統:MacOS 10.11
Python:系統自帶的2.7.10

  1. 第1步:下載MySQL-Python,並解壓
    下載地址:從sourceforge下載

    插曲說明
    sourceforge站點牛逼的做了系統識別,
    如果是Mac系統則下載的包為.tar的壓縮包,下載後解壓即可;
    如果是Win系統則下載的是exe的安裝包;
    慚愧,在這點上吃了大虧,當時不知道是系統哪裡出了錯,硬是隻給我下載exe包,折騰了半天,最後還是到

    GitHub上MySQLdb源下載了原始碼包來安裝的

  2. 第2步:修改site.cfg檔案
    解壓MySQL-Python後,按理應該直接執行安裝,可是如果直接用第3步的安裝,會出現mysql_config not found錯誤,要解決這個錯誤,在MySQL-python的安裝包中找到site.cfg檔案,開啟它,找到以下內容:

     # 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的地址,預設是遮蔽的狀態;
    去掉這一句前面的#,並將mysql_config地址改為/usr/local/mysql/bin/mysql_config,因為這是mysql_config預設的真實地址(mac安裝mysql預設這個地址下有mysql_config檔案,想確認的朋友可以Shift+Command+G前往這個目錄檢視),程式碼改後如下:

     mysql_config = /usr/local/mysql/bin/mysql_config
  3. 第2步:安裝python
    執行終端(Terminal)程式,在視窗中執行:

     $ python setup.py install

    執行安裝程式並提示成功後,MySQL-Python就安裝成功了,這個時候可在終端視窗驗證:

     $ Python
     $ import MySQLdb

    若沒有提示錯誤,即安裝成功!

  4. 報錯說明:網上教程裡說會遇到 Reason: image not found 的錯誤,(如果你遇到可以百度下解決方案),我沒有遇到這個報錯,不過卻遇到另一個報錯:

     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.

    經過百度後,終於知道原因,將命令改為:

     sudo python setup.py install

    然後輸入密碼,這樣就安裝成功了!
    參考來源:Strack overflow: Python - [Errno 13] Permission denied

額外收穫:知道了Python和MySQL的安裝地址
Python:/usr/bin/python
MySQL:/usr/local/mysql/
想確認的朋友可以Shift+Command+G前往這個目錄檢視



作者:大資料社群
連結:http://www.jianshu.com/p/71cf187598ce
來源:簡書
著作權歸作者所有。商業轉載請聯絡作者獲得授權,非商業轉載請註明出處。