Python Mac installs mysqlclient package error “error: command’clang’ failed with exit status 1” solution

Python Mac installation mysqlClient package error “Error: Command ‘clang’ failed with Exit Status 1

Today, I installed mysqlClient package on Mac, but it always gave me an error. I finally solved the problem for a long time, so I wrote down the solution


PIP installation command

pip install mysqlclient

Error message

Building wheels for collected packages: mysqlclient
  Building wheel for mysqlclient (setup.py) ... error
  ERROR: Complete output from command /Users/xxx/virtualSpaces/flask_info/bin/python3.7 -u -c 'import setuptools, tokenize;__file__='"'"'/private/var/folders/jg/_5c6yf2x2b721htvf0h3vvlm0000gn/T/pip-install-vke00caa/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/jg/_5c6yf2x2b721htvf0h3vvlm0000gn/T/pip-wheel-voixgzfs --python-tag cp37:
  ERROR: running bdist_wheel
  running build
  running build_py
  creating build
  creating build/lib.macosx-10.14-x86_64-3.7
  copying _mysql_exceptions.py -> 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/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
  copying MySQLdb/constants/REFRESH.py -> build/lib.macosx-10.14-x86_64-3.7/MySQLdb/constants
  running build_ext
  building '_mysql' extension
  creating build/temp.macosx-10.14-x86_64-3.7
  clang -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -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,3,13,'final',0) -D__version__=1.3.13 -I/usr/local/include -I/usr/local/Cellar/python/3.7.2/Frameworks/Python.framework/Versions/3.7/include/python3.7m -c _mysql.c -o build/temp.macosx-10.14-x86_64-3.7/_mysql.o
  _mysql.c:257:6: warning: assigning to 'char *' from 'const char *' discards qualifiers [-Wincompatible-pointer-types-discards-qualifiers]
                          s = PyUnicode_AsUTF8(item);
                            ^ ~~~~~~~~~~~~~~~~~~~~~~
  _mysql.c:287:6: warning: assigning to 'char *' from 'const char *' discards qualifiers [-Wincompatible-pointer-types-discards-qualifiers]
                          s = PyUnicode_AsUTF8(item);
                            ^ ~~~~~~~~~~~~~~~~~~~~~~
  _mysql.c:564:3: warning: assigning to 'char *' from 'const char *' discards qualifiers [-Wincompatible-pointer-types-discards-qualifiers]
                  _stringsuck(ca, value, ssl);
                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~
  _mysql.c:553:16: note: expanded from macro '_stringsuck'
          if(t){d=PyUnicode_AsUTF8(t);ssl_keepref[n_ssl_keepref++]=t;}\
                 ^~~~~~~~~~~~~~~~~~~~
  _mysql.c:565:3: warning: assigning to 'char *' from 'const char *' discards qualifiers [-Wincompatible-pointer-types-discards-qualifiers]
                  _stringsuck(capath, value, ssl);
                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  _mysql.c:553:16: note: expanded from macro '_stringsuck'
          if(t){d=PyUnicode_AsUTF8(t);ssl_keepref[n_ssl_keepref++]=t;}\
                 ^~~~~~~~~~~~~~~~~~~~
  _mysql.c:566:3: warning: assigning to 'char *' from 'const char *' discards qualifiers [-Wincompatible-pointer-types-discards-qualifiers]
                  _stringsuck(cert, value, ssl);
                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  _mysql.c:553:16: note: expanded from macro '_stringsuck'
          if(t){d=PyUnicode_AsUTF8(t);ssl_keepref[n_ssl_keepref++]=t;}\
                 ^~~~~~~~~~~~~~~~~~~~
  _mysql.c:567:3: warning: assigning to 'char *' from 'const char *' discards qualifiers [-Wincompatible-pointer-types-discards-qualifiers]
                  _stringsuck(key, value, ssl);
                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
  _mysql.c:553:16: note: expanded from macro '_stringsuck'
          if(t){d=PyUnicode_AsUTF8(t);ssl_keepref[n_ssl_keepref++]=t;}\
                 ^~~~~~~~~~~~~~~~~~~~
  _mysql.c:568:3: warning: assigning to 'char *' from 'const char *' discards qualifiers [-Wincompatible-pointer-types-discards-qualifiers]
                  _stringsuck(cipher, value, ssl);
                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  _mysql.c:553:16: note: expanded from macro '_stringsuck'
          if(t){d=PyUnicode_AsUTF8(t);ssl_keepref[n_ssl_keepref++]=t;}\
                 ^~~~~~~~~~~~~~~~~~~~
  _mysql.c:2530:8: warning: assigning to 'char *' from 'const char *' discards qualifiers [-Wincompatible-pointer-types-discards-qualifiers]
          cname = PyUnicode_AsUTF8(name);
                ^ ~~~~~~~~~~~~~~~~~~~~~~
  8 warnings generated.
  clang -bundle -undefined dynamic_lookup build/temp.macosx-10.14-x86_64-3.7/_mysql.o -L/usr/local/Cellar/mysql-connector-c/6.1.11/lib -lmysqlclient -lssl -lcrypto -o build/lib.macosx-10.14-x86_64-3.7/_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
  Running setup.py clean for mysqlclient
Failed to build mysqlclient
  Running setup.py install for mysqlclient ... error
    ERROR: Complete output from command /Users/xxx/virtualSpaces/flask_info/bin/python3.7 -u -c 'import setuptools, tokenize;__file__='"'"'/private/var/folders/jg/_5c6yf2x2b721htvf0h3vvlm0000gn/T/pip-install-vke00caa/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/jg/_5c6yf2x2b721htvf0h3vvlm0000gn/T/pip-record-nq6eazu6/install-record.txt --single-version-externally-managed --compile --install-headers /Users/xxx/virtualSpaces/flask_info/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
    copying _mysql_exceptions.py -> 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/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
    copying MySQLdb/constants/REFRESH.py -> build/lib.macosx-10.14-x86_64-3.7/MySQLdb/constants
    running build_ext
    building '_mysql' extension
    creating build/temp.macosx-10.14-x86_64-3.7
    clang -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -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,3,13,'final',0) -D__version__=1.3.13 -I/usr/local/include -I/usr/local/Cellar/python/3.7.2/Frameworks/Python.framework/Versions/3.7/include/python3.7m -c _mysql.c -o build/temp.macosx-10.14-x86_64-3.7/_mysql.o
    _mysql.c:257:6: warning: assigning to 'char *' from 'const char *' discards qualifiers [-Wincompatible-pointer-types-discards-qualifiers]
                            s = PyUnicode_AsUTF8(item);
                              ^ ~~~~~~~~~~~~~~~~~~~~~~
    _mysql.c:287:6: warning: assigning to 'char *' from 'const char *' discards qualifiers [-Wincompatible-pointer-types-discards-qualifiers]
                            s = PyUnicode_AsUTF8(item);
                              ^ ~~~~~~~~~~~~~~~~~~~~~~
    _mysql.c:564:3: warning: assigning to 'char *' from 'const char *' discards qualifiers [-Wincompatible-pointer-types-discards-qualifiers]
                    _stringsuck(ca, value, ssl);
                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~
    _mysql.c:553:16: note: expanded from macro '_stringsuck'
            if(t){d=PyUnicode_AsUTF8(t);ssl_keepref[n_ssl_keepref++]=t;}\
                   ^~~~~~~~~~~~~~~~~~~~
    _mysql.c:565:3: warning: assigning to 'char *' from 'const char *' discards qualifiers [-Wincompatible-pointer-types-discards-qualifiers]
                    _stringsuck(capath, value, ssl);
                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    _mysql.c:553:16: note: expanded from macro '_stringsuck'
            if(t){d=PyUnicode_AsUTF8(t);ssl_keepref[n_ssl_keepref++]=t;}\
                   ^~~~~~~~~~~~~~~~~~~~
    _mysql.c:566:3: warning: assigning to 'char *' from 'const char *' discards qualifiers [-Wincompatible-pointer-types-discards-qualifiers]
                    _stringsuck(cert, value, ssl);
                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    _mysql.c:553:16: note: expanded from macro '_stringsuck'
            if(t){d=PyUnicode_AsUTF8(t);ssl_keepref[n_ssl_keepref++]=t;}\
                   ^~~~~~~~~~~~~~~~~~~~
    _mysql.c:567:3: warning: assigning to 'char *' from 'const char *' discards qualifiers [-Wincompatible-pointer-types-discards-qualifiers]
                    _stringsuck(key, value, ssl);
                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
    _mysql.c:553:16: note: expanded from macro '_stringsuck'
            if(t){d=PyUnicode_AsUTF8(t);ssl_keepref[n_ssl_keepref++]=t;}\
                   ^~~~~~~~~~~~~~~~~~~~
    _mysql.c:568:3: warning: assigning to 'char *' from 'const char *' discards qualifiers [-Wincompatible-pointer-types-discards-qualifiers]
                    _stringsuck(cipher, value, ssl);
                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    _mysql.c:553:16: note: expanded from macro '_stringsuck'
            if(t){d=PyUnicode_AsUTF8(t);ssl_keepref[n_ssl_keepref++]=t;}\
                   ^~~~~~~~~~~~~~~~~~~~
    _mysql.c:2530:8: warning: assigning to 'char *' from 'const char *' discards qualifiers [-Wincompatible-pointer-types-discards-qualifiers]
            cname = PyUnicode_AsUTF8(name);
                  ^ ~~~~~~~~~~~~~~~~~~~~~~
    8 warnings generated.
    clang -bundle -undefined dynamic_lookup build/temp.macosx-10.14-x86_64-3.7/_mysql.o -L/usr/local/Cellar/mysql-connector-c/6.1.11/lib -lmysqlclient -lssl -lcrypto -o build/lib.macosx-10.14-x86_64-3.7/_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/xxx/virtualSpaces/flask_info/bin/python3.7 -u -c 'import setuptools, tokenize;__file__='"'"'/private/var/folders/jg/_5c6yf2x2b721htvf0h3vvlm0000gn/T/pip-install-vke00caa/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/jg/_5c6yf2x2b721htvf0h3vvlm0000gn/T/pip-record-nq6eazu6/install-record.txt --single-version-externally-managed --compile --install-headers /Users/xxx/virtualSpaces/flask_info/bin/../include/site/python3.7/mysqlclient" failed with error code 1 in /private/var/folders/jg/_5c6yf2x2b721htvf0h3vvlm0000gn/T/pip-install-vke00caa/mysqlclient/

The solution
Github gives an explanation of brew info openssl :
solves the problem by adding environment variables:

export LDFLAGS="-L/usr/local/opt/openssl/lib"
export CPPFLAGS="-I/usr/local/opt/openssl/include"

Then execute the installation command again

pip install mysqlclient

On my side, I installed the reset.txt file directly, but the difference is not significant.


Making related issue official solution:

reference: https://github.com/PyMySQL/mysqlclient-python/issues/169
https://www.jianshu.com/p/d4a349a7e7f5

Read More: