后端的搭建就有很大的坑啦!!!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