pipenvでmysqlclientを入れようとしたときに詰まったときの対処法

pipenvでmysqlclientを入れようとしたときに↓のエラーが出てインストールできなくて困ったのでメモしておきます。

[pipenv.exceptions.InstallError]:   File "/usr/local/Cellar/pipenv/2018.11.26_2/libexec/lib/python3.7/site-packages/pipenv/core.py", line 1874, in do_install
[pipenv.exceptions.InstallError]:       keep_outdated=keep_outdated
[pipenv.exceptions.InstallError]:   File "/usr/local/Cellar/pipenv/2018.11.26_2/libexec/lib/python3.7/site-packages/pipenv/core.py", line 1253, in do_init
[pipenv.exceptions.InstallError]:       pypi_mirror=pypi_mirror,
[pipenv.exceptions.InstallError]:   File "/usr/local/Cellar/pipenv/2018.11.26_2/libexec/lib/python3.7/site-packages/pipenv/core.py", line 859, in do_install_dependencies
[pipenv.exceptions.InstallError]:       retry_list, procs, failed_deps_queue, requirements_dir, **install_kwargs
[pipenv.exceptions.InstallError]:   File "/usr/local/Cellar/pipenv/2018.11.26_2/libexec/lib/python3.7/site-packages/pipenv/core.py", line 763, in batch_install
[pipenv.exceptions.InstallError]:       _cleanup_procs(procs, not blocking, failed_deps_queue, retry=retry)
[pipenv.exceptions.InstallError]:   File "/usr/local/Cellar/pipenv/2018.11.26_2/libexec/lib/python3.7/site-packages/pipenv/core.py", line 681, in _cleanup_procs
[pipenv.exceptions.InstallError]:       raise exceptions.InstallError(c.dep.name, extra=err_lines)
[pipenv.exceptions.InstallError]: ['Collecting mysqlclient==1.4.2.post1 (from -r /var/folders/g9/vlfxdgw17ngd6tcr5kg8twzh0000gn/T/pipenv-47rh3cfu-requirements/pipenv-n0ib3nno-requirement.txt (line 1))', '  Using cached https://files.pythonhosted.org/packages/f4/f1/3bb6f64ca7a429729413e6556b7ba5976df06019a5245a43d36032f1061e/mysqlclient-1.4.2.post1.tar.gz', 'Building wheels for collected packages: mysqlclient', '  Building wheel for mysqlclient (setup.py): started', "  Building wheel for mysqlclient (setup.py): finished with status 'error'", '  Running setup.py clean for mysqlclient', 'Failed to build mysqlclient', 'Installing collected packages: mysqlclient', '  Running setup.py install for mysqlclient: started', "    Running setup.py install for mysqlclient: finished with status 'error'"]
[pipenv.exceptions.InstallError]: ['ERROR: Complete output from command /Users/hp29/PycharmProjects/Saffron/calyx/src/.venv/bin/python3.7 -u -c \'import setuptools, tokenize;__file__=\'"\'"\'/private/var/folders/g9/vlfxdgw17ngd6tcr5kg8twzh0000gn/T/pip-install-znoxgvv7/mysqlclient/setup.py\'"\'"\';f=getattr(tokenize, \'"\'"\'open\'"\'"\', open)(__file__);code=f.read().replace(\'"\'"\'\\r\\n\'"\'"\', \'"\'"\'\\n\'"\'"\');f.close();exec(compile(code, __file__, \'"\'"\'exec\'"\'"\'))\' bdist_wheel -d /private/var/folders/g9/vlfxdgw17ngd6tcr5kg8twzh0000gn/T/pip-wheel-n0j6235l --python-tag cp37:', '  ERROR: running bdist_wheel', '  running build', '  running build_py', '  creating build', '  creating build/lib.macosx-10.14-x86_64-3.7', '  creating build/lib.macosx-10.14-x86_64-3.7/MySQLdb', '  copying MySQLdb/__init__.py -> build/lib.macosx-10.14-x86_64-3.7/MySQLdb', '  copying MySQLdb/_exceptions.py -> build/lib.macosx-10.14-x86_64-3.7/MySQLdb', '  copying MySQLdb/compat.py -> build/lib.macosx-10.14-x86_64-3.7/MySQLdb', '  copying MySQLdb/connections.py -> build/lib.macosx-10.14-x86_64-3.7/MySQLdb', '  copying MySQLdb/converters.py -> build/lib.macosx-10.14-x86_64-3.7/MySQLdb', '  copying MySQLdb/cursors.py -> build/lib.macosx-10.14-x86_64-3.7/MySQLdb', '  copying MySQLdb/release.py -> build/lib.macosx-10.14-x86_64-3.7/MySQLdb', '  copying MySQLdb/times.py -> build/lib.macosx-10.14-x86_64-3.7/MySQLdb', '  creating build/lib.macosx-10.14-x86_64-3.7/MySQLdb/constants', '  copying MySQLdb/constants/__init__.py -> build/lib.macosx-10.14-x86_64-3.7/MySQLdb/constants', '  copying MySQLdb/constants/CLIENT.py -> build/lib.macosx-10.14-x86_64-3.7/MySQLdb/constants', '  copying MySQLdb/constants/CR.py -> build/lib.macosx-10.14-x86_64-3.7/MySQLdb/constants', '  copying MySQLdb/constants/ER.py -> build/lib.macosx-10.14-x86_64-3.7/MySQLdb/constants', '  copying MySQLdb/constants/FIELD_TYPE.py -> build/lib.macosx-10.14-x86_64-3.7/MySQLdb/constants', '  copying MySQLdb/constants/FLAG.py -> build/lib.macosx-10.14-x86_64-3.7/MySQLdb/constants', '  warning: build_py: byte-compiling is disabled, skipping.', '  ', '  running build_ext', "  building 'MySQLdb._mysql' extension", '  creating build/temp.macosx-10.14-x86_64-3.7', '  creating build/temp.macosx-10.14-x86_64-3.7/MySQLdb', "  clang -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/include -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/System/Library/Frameworks/Tk.framework/Versions/8.5/Headers -Dversion_info=(1,4,2,'post',1) -D__version__=1.4.2.post1 -I/usr/local/opt/mysql@5.7/include/mysql -I/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/include/python3.7m -c MySQLdb/_mysql.c -o build/temp.macosx-10.14-x86_64-3.7/MySQLdb/_mysql.o", '  clang -bundle -undefined dynamic_lookup -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk build/temp.macosx-10.14-x86_64-3.7/MySQLdb/_mysql.o -L/usr/local/opt/mysql@5.7/lib -lmysqlclient -lssl -lcrypto -o build/lib.macosx-10.14-x86_64-3.7/MySQLdb/_mysql.cpython-37m-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: Failed building wheel for mysqlclient', '    ERROR: Complete output from command /Users/hp29/PycharmProjects/Saffron/calyx/src/.venv/bin/python3.7 -u -c \'import setuptools, tokenize;__file__=\'"\'"\'/private/var/folders/g9/vlfxdgw17ngd6tcr5kg8twzh0000gn/T/pip-install-znoxgvv7/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/g9/vlfxdgw17ngd6tcr5kg8twzh0000gn/T/pip-record-8oe1bf42/install-record.txt --single-version-externally-managed --compile --install-headers /Users/hp29/PycharmProjects/Saffron/calyx/src/.venv/bin/../include/site/python3.7/mysqlclient:', '    ERROR: running install', '    running build', '    running build_py', '    creating build', '    creating build/lib.macosx-10.14-x86_64-3.7', '    creating build/lib.macosx-10.14-x86_64-3.7/MySQLdb', '    copying MySQLdb/__init__.py -> build/lib.macosx-10.14-x86_64-3.7/MySQLdb', '    copying MySQLdb/_exceptions.py -> build/lib.macosx-10.14-x86_64-3.7/MySQLdb', '    copying MySQLdb/compat.py -> build/lib.macosx-10.14-x86_64-3.7/MySQLdb', '    copying MySQLdb/connections.py -> build/lib.macosx-10.14-x86_64-3.7/MySQLdb', '    copying MySQLdb/converters.py -> build/lib.macosx-10.14-x86_64-3.7/MySQLdb', '    copying MySQLdb/cursors.py -> build/lib.macosx-10.14-x86_64-3.7/MySQLdb', '    copying MySQLdb/release.py -> build/lib.macosx-10.14-x86_64-3.7/MySQLdb', '    copying MySQLdb/times.py -> build/lib.macosx-10.14-x86_64-3.7/MySQLdb', '    creating build/lib.macosx-10.14-x86_64-3.7/MySQLdb/constants', '    copying MySQLdb/constants/__init__.py -> build/lib.macosx-10.14-x86_64-3.7/MySQLdb/constants', '    copying MySQLdb/constants/CLIENT.py -> build/lib.macosx-10.14-x86_64-3.7/MySQLdb/constants', '    copying MySQLdb/constants/CR.py -> build/lib.macosx-10.14-x86_64-3.7/MySQLdb/constants', '    copying MySQLdb/constants/ER.py -> build/lib.macosx-10.14-x86_64-3.7/MySQLdb/constants', '    copying MySQLdb/constants/FIELD_TYPE.py -> build/lib.macosx-10.14-x86_64-3.7/MySQLdb/constants', '    copying MySQLdb/constants/FLAG.py -> build/lib.macosx-10.14-x86_64-3.7/MySQLdb/constants', '    warning: build_py: byte-compiling is disabled, skipping.', '    ', '    running build_ext', "    building 'MySQLdb._mysql' extension", '    creating build/temp.macosx-10.14-x86_64-3.7', '    creating build/temp.macosx-10.14-x86_64-3.7/MySQLdb', "    clang -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/usr/include -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/System/Library/Frameworks/Tk.framework/Versions/8.5/Headers -Dversion_info=(1,4,2,'post',1) -D__version__=1.4.2.post1 -I/usr/local/opt/mysql@5.7/include/mysql -I/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/include/python3.7m -c MySQLdb/_mysql.c -o build/temp.macosx-10.14-x86_64-3.7/MySQLdb/_mysql.o", '    clang -bundle -undefined dynamic_lookup -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk build/temp.macosx-10.14-x86_64-3.7/MySQLdb/_mysql.o -L/usr/local/opt/mysql@5.7/lib -lmysqlclient -lssl -lcrypto -o build/lib.macosx-10.14-x86_64-3.7/MySQLdb/_mysql.cpython-37m-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 "/Users/hp29/PycharmProjects/Saffron/calyx/src/.venv/bin/python3.7 -u -c \'import setuptools, tokenize;__file__=\'"\'"\'/private/var/folders/g9/vlfxdgw17ngd6tcr5kg8twzh0000gn/T/pip-install-znoxgvv7/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/g9/vlfxdgw17ngd6tcr5kg8twzh0000gn/T/pip-record-8oe1bf42/install-record.txt --single-version-externally-managed --compile --install-headers /Users/hp29/PycharmProjects/Saffron/calyx/src/.venv/bin/../include/site/python3.7/mysqlclient" failed with error code 1 in /private/var/folders/g9/vlfxdgw17ngd6tcr5kg8twzh0000gn/T/pip-install-znoxgvv7/mysqlclient/']

2017年7月6日のバグレポートで bugs.mysql.com のように mysql_config内で

# Create options
libs="-L$pkglibdir"
libs="$libs -lmysqlclient -lssl -lcrypto"

となっていなければならないところが

# on macOS, on or about line 112:
# Create options
libs="-L$pkglibdir"
libs="$libs -l "

となっていて修正されたことがわかります。

それに伴って、コンパイラがopensslを見つけられるように

$ echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ~/.bash_profile(既にあるなら不要)
$ export LDFLAGS="-L/usr/local/opt/openssl/lib"
$ export CPPFLAGS="-I/usr/local/opt/openssl/include"

をしてやる必要があります。(brew info opensslで確認できる)

この環境変数を設定してやることでうまくインストールできました!

参考

medium.com