Error Installing MySqlClient

Mac 在 pyenv 虚拟环境中安装 mysqlclient 出现错误解决办法。

Posted by Dusign on 2019-12-20
Words 827 and Reading Time 5 Minutes
Viewed Times

Django 使用 mysql 时,需要安装 mysqlclient 或 pymysql ,笔者的系统是 MACOS ,用 pyenv 管理 python 版本,使用 pyenv-virtualenv 搭建 python 虚拟环境,python 版本是 python3.8,虚拟环境中安装 mysqlclient 时出现了报错,错误及解决方法如下。

Error

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
Collecting mysqlclient
Using cached https://files.pythonhosted.org/packages/d0/97/7326248ac8d5049968bf4ec708a5d3d4806e412a42e74160d7f266a3e03a/mysqlclient-1.4.6.tar.gz
Installing collected packages: mysqlclient
Running setup.py install for mysqlclient ... error
ERROR: Command errored out with exit status 1:
command: /Users/dugang/.pyenv/versions/3.8.0/envs/myow/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/g1/ht4cq7sx40vcbll3gw_bncgh0000gn/T/pip-install-cytmezc5/mysqlclient/setup.py'"'"'; __file__='"'"'/private/var/folders/g1/ht4cq7sx40vcbll3gw_bncgh0000gn/T/pip-install-cytmezc5/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 /private/var/folders/g1/ht4cq7sx40vcbll3gw_bncgh0000gn/T/pip-record-cmhc1r6g/install-record.txt --single-version-externally-managed --compile --install-headers /Users/dugang/.pyenv/versions/3.8.0/envs/myow/include/site/python3.8/mysqlclient
cwd: /private/var/folders/g1/ht4cq7sx40vcbll3gw_bncgh0000gn/T/pip-install-cytmezc5/mysqlclient/
Complete output (30 lines):
running install
running build
running build_py
creating build
creating build/lib.macosx-10.15-x86_64-3.8
creating build/lib.macosx-10.15-x86_64-3.8/MySQLdb
copying MySQLdb/__init__.py -> build/lib.macosx-10.15-x86_64-3.8/MySQLdb
copying MySQLdb/_exceptions.py -> build/lib.macosx-10.15-x86_64-3.8/MySQLdb
copying MySQLdb/compat.py -> build/lib.macosx-10.15-x86_64-3.8/MySQLdb
copying MySQLdb/connections.py -> build/lib.macosx-10.15-x86_64-3.8/MySQLdb
copying MySQLdb/converters.py -> build/lib.macosx-10.15-x86_64-3.8/MySQLdb
copying MySQLdb/cursors.py -> build/lib.macosx-10.15-x86_64-3.8/MySQLdb
copying MySQLdb/release.py -> build/lib.macosx-10.15-x86_64-3.8/MySQLdb
copying MySQLdb/times.py -> build/lib.macosx-10.15-x86_64-3.8/MySQLdb
creating build/lib.macosx-10.15-x86_64-3.8/MySQLdb/constants
copying MySQLdb/constants/__init__.py -> build/lib.macosx-10.15-x86_64-3.8/MySQLdb/constants
copying MySQLdb/constants/CLIENT.py -> build/lib.macosx-10.15-x86_64-3.8/MySQLdb/constants
copying MySQLdb/constants/CR.py -> build/lib.macosx-10.15-x86_64-3.8/MySQLdb/constants
copying MySQLdb/constants/ER.py -> build/lib.macosx-10.15-x86_64-3.8/MySQLdb/constants
copying MySQLdb/constants/FIELD_TYPE.py -> build/lib.macosx-10.15-x86_64-3.8/MySQLdb/constants
copying MySQLdb/constants/FLAG.py -> build/lib.macosx-10.15-x86_64-3.8/MySQLdb/constants
running build_ext
building 'MySQLdb._mysql' extension
creating build/temp.macosx-10.15-x86_64-3.8
creating build/temp.macosx-10.15-x86_64-3.8/MySQLdb
clang -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -Dversion_info=(1,4,6,'final',0) -D__version__=1.4.6 -I/usr/local/Cellar/mariadb/10.4.6_1/include/mysql -I/usr/local/Cellar/mariadb/10.4.6_1/include/mysql/.. -I/Users/dugang/.pyenv/versions/3.8.0/envs/myow/include -I/Users/dugang/.pyenv/versions/3.8.0/include/python3.8 -c MySQLdb/_mysql.c -o build/temp.macosx-10.15-x86_64-3.8/MySQLdb/_mysql.o
clang -bundle -undefined dynamic_lookup -L/usr/local/opt/readline/lib -L/usr/local/opt/readline/lib -L/Users/dugang/.pyenv/versions/3.8.0/lib -L/usr/local/opt/readline/lib -L/usr/local/opt/readline/lib -L/Users/dugang/.pyenv/versions/3.8.0/lib build/temp.macosx-10.15-x86_64-3.8/MySQLdb/_mysql.o -L/usr/local/Cellar/mariadb/10.4.6_1/lib -lmariadb -lz -liconv -lssl -lcrypto -o build/lib.macosx-10.15-x86_64-3.8/MySQLdb/_mysql.cpython-38-darwin.so
ld: library not found for -lssl
clang: error: linker command failed with exit code 1 (use -v to see invocation)
error: command 'clang' failed with exit status 1
----------------------------------------
ERROR: Command errored out with exit status 1: /Users/dugang/.pyenv/versions/3.8.0/envs/myow/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/g1/ht4cq7sx40vcbll3gw_bncgh0000gn/T/pip-install-cytmezc5/mysqlclient/setup.py'"'"'; __file__='"'"'/private/var/folders/g1/ht4cq7sx40vcbll3gw_bncgh0000gn/T/pip-install-cytmezc5/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 /private/var/folders/g1/ht4cq7sx40vcbll3gw_bncgh0000gn/T/pip-record-cmhc1r6g/install-record.txt --single-version-externally-managed --compile --install-headers /Users/dugang/.pyenv/versions/3.8.0/envs/myow/include/site/python3.8/mysqlclient Check the logs for full command output.

Solution

1
2
brew install openssl
export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/opt/openssl/lib/

If you like this blog or find it useful for you, you are welcome to comment on it. You are also welcome to share this blog, so that more people can participate in it. If the images used in the blog infringe your copyright, please contact the author to delete them. Thank you !

...

...

00:00
00:00