後端的搭建就有很大的坑啦!!!python
在wiki中的安裝依賴包 pip install -r req.txt 步驟中,req.txt裏有個MySQL-python包,在有些機器上直接用pip會報各類奇奇怪怪的錯誤,例如:mysql
_mysql.c:44:10: fatal error: 'my_config.h' file not found
error: command 'cc' failed with exit status 1
EnvironmentError: mysql_config not found
File "/Users/ss/Downloads/MySQL-python-1.2.5/distribute_setup.py", line 116, in _build_egg
raise IOError('Could not build the egg.')
出現那麼多問題究其根源居然是由mysql官方引發的(驚喜不?),具體可參考博客。
看起來官方是不太可能修復了,因此還得由本身解決。sql
參考了博客 - mac os x 10.8 安裝python-mysqldb血淚史後,通過大半天的折騰,終於安裝成功了,現分享以下:後端
https://www.easegamer.com/?p=545xcode
bash
brew uninstall mysql
brew uninstall mysql-connector-c
fetch
2. 安裝mysqlui
mac下安裝mysql有兩種方式,使用安裝包安裝或使用使用homebrew安裝
這裏推薦使用安裝包安裝,由於用homwbrew安裝後啓動mysql服務又會出現莫名其妙的錯誤。不怕麻煩的能夠本身體會下~url
安裝時注意如下幾點:spa
改爲
mysql_config = /usr/local/mysql/bin/mysql_config
/usr/local/mysql 是安裝mysql的路徑
sudo ARCHFLAGS=-Wno-error=unused-command-line-argument-hard-error-in-future python setup.py build
安裝完後還記得添加環境變量
export DYLD_LIBRARY_PATH=/usr/local/mysql/lib:$DYLD_LIBRARY_PATH
在終端依次輸入如下代碼,若是每步都沒提示錯,那恭喜你安裝MySQL-python成功~
1
2
3
4
5
6
7
8
9
10
11
|
python
>>>
import
MySQLdb
>>> conn
=
MySQLdb.connect(host
=
"localhost"
,user
=
"root"
,passwd
=
"123"
,db
=
"mysql"
)
>>> cursor
=
conn.cursor()
>>> sql
=
"select * from user"
>>> cursor.execute(sql)
2L
>>> row
=
cursor.fetchone()
>>>
print
row
(
'localhost'
,
'root'
,
'Y'
,
'Y'
,
'Y'
,
'Y'
,
'Y'
,
'Y'
,
'Y'
,
'Y'
,
'Y'
,
'Y'
,
'Y'
,
'Y'
,
'Y'
,
'Y'
,
'Y'
,
'Y'
,
'Y'
,
'Y'
,
'Y'
,
'Y'
,
'Y'
,
'Y'
,
'Y'
,
'Y'
,
'Y'
,
'Y'
,
'Y'
,
'Y'
,
'Y'
, '
', '
', '
', '
', 0L, 0L, 0L, 0L, '
mysql_native_password
', '
*
23AE809DDACAF96AF0FD78ED04B6A265E05AA257
', '
N
', datetime.datetime(2018, 7, 16, 3, 48, 8), None, '
N')
>>>
|
但若是import MySQLdb時就出現錯誤,且錯誤最後一行寫着 Reason: image not found,可嘗試用如下命令來解決 sudo ln -s /usr/local/mysql/lib/libmysqlclient.20.dylib /usr/lib/libmysqlclient.20.dylib