问题背景:
Mac下打开terminal,输入以下代码安装mysqlclient
pip3 install mysqlclient
输出错误日志内容:
Using cached mysqlclient-1.3.12.tar.gz
Installing collected packages: mysqlclient
Running setup.py install for mysqlclient: started
Running setup.py install for mysqlclient: finished with status 'error'
Complete output from command /Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 -u -c "import setuptools, tokenize;__file__='/private/var/folders/4k/68mf14qn3gx0pb4wt6hg6p7r0000gn/T/pycharm-packaging/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/4k/68mf14qn3gx0pb4wt6hg6p7r0000gn/T/pip-e1yq85ib-record/install-record.txt --single-version-externally-managed --compile:
running install
running build
running build_py
creating build
creating build/lib.macosx-10.6-intel-3.6
copying _mysql_exceptions.py -> build/lib.macosx-10.6-intel-3.6
creating build/lib.macosx-10.6-intel-3.6/MySQLdb
copying MySQLdb/__init__.py -> build/lib.macosx-10.6-intel-3.6/MySQLdb
copying MySQLdb/compat.py -> build/lib.macosx-10.6-intel-3.6/MySQLdb
copying MySQLdb/connections.py -> build/lib.macosx-10.6-intel-3.6/MySQLdb
copying MySQLdb/converters.py -> build/lib.macosx-10.6-intel-3.6/MySQLdb
copying MySQLdb/cursors.py -> build/lib.macosx-10.6-intel-3.6/MySQLdb
copying MySQLdb/release.py -> build/lib.macosx-10.6-intel-3.6/MySQLdb
copying MySQLdb/times.py -> build/lib.macosx-10.6-intel-3.6/MySQLdb
creating build/lib.macosx-10.6-intel-3.6/MySQLdb/constants
copying MySQLdb/constants/__init__.py -> build/lib.macosx-10.6-intel-3.6/MySQLdb/constants
copying MySQLdb/constants/CLIENT.py -> build/lib.macosx-10.6-intel-3.6/MySQLdb/constants
copying MySQLdb/constants/CR.py -> build/lib.macosx-10.6-intel-3.6/MySQLdb/constants
copying MySQLdb/constants/ER.py -> build/lib.macosx-10.6-intel-3.6/MySQLdb/constants
copying MySQLdb/constants/FIELD_TYPE.py -> build/lib.macosx-10.6-intel-3.6/MySQLdb/constants
copying MySQLdb/constants/FLAG.py -> build/lib.macosx-10.6-intel-3.6/MySQLdb/constants
copying MySQLdb/constants/REFRESH.py -> build/lib.macosx-10.6-intel-3.6/MySQLdb/constants
warning: build_py: byte-compiling is disabled, skipping.
running build_ext
building '_mysql' extension
creating build/temp.macosx-10.6-intel-3.6
/usr/bin/clang -fno-strict-aliasing -Wsign-compare -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -arch i386 -arch x86_64 -g -Dversion_info=(1,3,12,'final',0) -D__version__=1.3.12 -I/usr/local/Cellar/mysql/5.7.21/include/mysql -I/Library/Frameworks/Python.framework/Versions/3.6/include/python3.6m -c _mysql.c -o build/temp.macosx-10.6-intel-3.6/_mysql.o
/usr/bin/clang -bundle -undefined dynamic_lookup -arch i386 -arch x86_64 -g build/temp.macosx-10.6-intel-3.6/_mysql.o -L/usr/local/Cellar/mysql/5.7.21/lib -lmysqlclient -lssl -lcrypto -o build/lib.macosx-10.6-intel-3.6/_mysql.cpython-36m-darwin.so
ld: library not found for -lssl
clang: error: linker command failed with exit code 1 (use -v to see invocation)
error: command '/usr/bin/clang' failed with exit status 1
网上搜索了很多未找到明确解决方案,最终到官方github相关issue中找到解决方法:
Github给出brew info openssl
的解释:
解决方法:
添加环境变量:
export LDFLAGS="-L/usr/local/opt/openssl/lib"
export CPPFLAGS="-I/usr/local/opt/openssl/include"
环境变量创建、编辑、保存:
启动终端Terminal
-
进入当前用户的home目录
输入cd ~
-
创建.bash_profile
输入touch .bash_profile
-
编辑.bash_profile文件
输入open -e .bash_profile
保存文件,关闭.bash_profile
-
更新刚配置的环境变量
输入source .bash_profile
最终重新输入pip3 install mysqlclient
可看到成功安装