Mac OS下安装mysqlclient遇到的一些坑
在玩django的同时,必须需要mysqlclient和pillow包,想在本地Mac上装上mysqlclient,但着实遇到不少坑,最终还是在github issue中找到了解决方法,这里记录一下,也让遇到同样问题的朋友快速解决。
首先,需要安装mysql,并依赖openssl,这个就不说了,直接官网下载mysql安装包无脑安装就好了
1、这时,你想试一下安装mysqlclient,于是通过pip进行安装尝试:
pip3 install mysqlclient
这时你会得到一个错误:
Command "python setup.py egg_info" failed with error code 1 in /private/var/folders/0h/jn4vphd94wsf0sl01jb74p200000gn/T/pip-build-75a5emrb/mysqlclient/
2.别急,照着步骤做:通过brew安装 mysql-connector-c
brew install mysql-connector-c
3.安装后你可以找到mysql_config这个源文件,在里面找到如下内容
可以通过which mysql_config去查找
# Create options libs="-L$pkglibdir" libs="$libs -l "
将他替换成
# Create options libs="-L$pkglibdir" libs="$libs -lmysqlclient -lssl -lcrypto"
这时尝试安装mysqlclient,又出现另一个错误
Command "/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 -u -c "import
setuptools, tokenize;__file__='/private/var/folders/0h/jn4vphd94wsf0sl01jb74p200000gn/T/pip-build-7dif93uz/mysqlclient/setup.py';
f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))"
install --record /var/folders/0h/jn4vphd94wsf0sl01jb74p200000gn/T/pip-ljzyp5xt-record/install-record.txt --single-version-externally-managed --compile"
failed with error code 1 in /private/var/folders/0h/jn4vphd94wsf0sl01jb74p200000gn/T/pip-build-7dif93uz/mysqlclient/
不要急,这时因为没有openssl环境变量,这样做
vim ~/.bash_profile
export LDFLAGS="-L/usr/local/opt/openssl/lib" export CPPFLAGS="-I/usr/local/opt/openssl/include"
添加完之后source一下
最后,再次安装mysqlclient
pip3 install mysqlclient 你会看到 Collecting mysqlclient Using cached mysqlclient-1.3.12.tar.gz Installing collected packages: mysqlclient Running setup.py install for mysqlclient ... done Successfully installed mysqlclient-1.3.12
这样就ok了