mysql-python可以说是我遇到的python第二大坑了(第一是编码),之前用Ubuntu的被虐千百遍之后记下了一行牛逼的代码:
sudo apt-get install libmysqlclient-dev
如今换了mac,没想到又栽到这上面。
刚开始是直接用pip安装:
pip install mysql-python
出错,提示没有找到mysql_config:
Collecting MySQL-python
Using cached <https://files.pythonhosted.org/packages/a5/e9/51b544da85a36a68debe7a7091f068d802fc515a3a202652828c73453cad/MySQL-python-1.2.5.zip>
Complete output from command python setup.py egg_info:
sh: mysql_config: command not found
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/private/var/folders/c3/j0d4n2qj19bc9909ql20qmnr0000gn/T/pip-install-1d9DIn/MySQL-python/setup.py", line 17, in <module>
metadata, options = get_config()
File "setup_posix.py", line 43, in get_config
libs = mysql_config("libs_r")
File "setup_posix.py", line 25, in mysql_config
raise EnvironmentError("%s not found" % (mysql_config.path,))
EnvironmentError: mysql_config not found
----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /private/var/folders/c3/j0d4n2qj19bc9909ql20qmnr0000gn/T/pip-install-1d9DIn/MySQL-python/
然后网上说要装mysql,遂装之:
brew install mysql
然后剧情就发展为这样了:
_mysql.c:44:10: fatal error: 'my_config.h' file not found
#include "my_config.h"
^~~~~~~~~~~~~
1 error generated.
error: command 'clang' failed with exit status 1
----------------------------------------
Failed building wheel for MySQL-python
Running setup.py clean for MySQL-python
Failed to build MySQL-python
紧接着看到某位大佬说要自己改文件装,原文链接,于是自己下载了,不要问我在哪里下载的:
解压之后进去修改site.cfg文件,找到 #mysql_config = /usr/local/bin/mysql_config
之后去掉注释,然后运行安装命令:
python setup.py install
# 如果用了虚拟环境,请用虚拟环境里面的python运行
/Users/jiy/myproject/venv/bin/python setup.py install
不出意外的话,还是会报 _mysql.c:44:10: fatal error: 'my_config.h' file not found
这是因为mysql_python里面的_mysql.c依赖my_config.h但是没有找到,这时候可以安装mysql-connector-c并拷贝my_config.h到当前位置,不想安装的话也可以直接从网上下载
brew install mysql-connector-c
cp /usr/local/opt/mysql-connector-c/include/my_config.h ./
# 或者
wget <https://raw.githubusercontent.com/ketzusaka/mysql-connector-c/master/Sources/include/my_config.h>
然后再