在MAC下 Python+Django+mysql配置

來源:互聯網
上載者:User

標籤:style   blog   http   io   color   os   ar   使用   for   

今天在搭建Django+mysql環境的時候遇到了一點問題,記錄下來。

 

安裝環境:OS X 10.10作業系統,Python 2.7。

MySQLdb其實包含在MySQL-python包中,因此無論下載還是在pip中search,都應該是搜尋MySQL-python。

 

以下將說明MySQLdb兩種常見的安裝方式:

下載安裝或者pip安裝MySQL-python。 

源碼安裝

下載MySQLdb源碼

下面是1.2.5的版本

https://pypi.python.org/packages/source/M/MySQL-python/MySQL-python-1.2.5.zip#md5=654f75b302db6ed8dc5a898c625e030c

下載後解壓,然後在終端Terminal中執行以下命令:

$ cd MySQL-python-1.2.5

 然後修改 site.cfg, 修改下面內容:

由#mysql_config = /usr/local/bin/mysql_config

改成mysql_config = /usr/local/mysql/bin/mysql_config

否則會出現找不到 MySQL config 的問題:

File "/tmp/easy_install-nHSsgl/MySQL-python-1.2.2/setup_posix.py", line 24, in mysql_config
EnvironmentError: mysql_config not found

然後修改 _mysql.c, 把第 37 到 39 行注釋掉, 如下:

//#ifndef uint//#define uint unsigned int//#endif


否則會出現:

In file included from /usr/local/mysql/include/mysql.h:47,                 from _mysql.c:40:/usr/include/sys/types.h:92: error: duplicate ‘unsigned‘/usr/include/sys/types.h:92: error: two or more data types in declaration specifierserror: command ‘gcc‘ failed with exit status 1

然後再用 python ./setup.py build 編譯

$ python ./setup.py build

然後再用 python ./setup.py install 安裝

$ sudo python ./setup.py installPassword:
使用pip安裝MySQLdb

在終端中執行:$ pip install MySQL-python

使用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進行選擇。

另外再提供一個pip安裝時找不到mysql_config的解決方案,在終端中輸入以下命令:

$ sudo ln -s /usr/local/mysql/bin/* /usr/bin

到這裡,MySQL-python包應該基本順利安裝。

 

解決 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

測試

用下面的命令進行測試:

$ cd ~$ pythonPython 2.5.1 (r251:54863, Apr 15 2008, 22:57:26) [GCC 4.0.1 (Apple Inc. build 5465)] on darwinType "help", "copyright", "credits" or "license" for more information.>>> import MySQLdb>>> MySQLdb.apilevel‘2.0‘>>> import django>>> print django.VERSION(1, 0, ‘final‘) 
常見錯誤

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

經網上查證: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

在MAC下 Python+Django+mysql配置

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.