mac安装mysqlclient执行命令:


pip install -i https://pypi.douban.com/simple mysqlclient
报错如下:



  ERROR: Command errored out with exit status 1:
   command: /opt/homebrew/opt/python@3.9/bin/python3.9 -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/tmp/pip-install-fa9eyul5/mysqlclient_31eb227649674b20a8b353f244cce7a6/setup.py'"'"'; __file__='"'"'/private/tmp/pip-install-fa9eyul5/mysqlclient_31eb227649674b20a8b353f244cce7a6/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d /private/tmp/pip-wheel-pmjp9ce8
       cwd: /private/tmp/pip-install-fa9eyul5/mysqlclient_31eb227649674b20a8b353f244cce7a6/
  Complete output (43 lines):
  mysql_config --version
  ['8.0.26']
  mysql_config --libs
  ['-L/opt/homebrew/opt/mysql/lib', '-lmysqlclient', '-lz', '-lzstd', '-lssl', '-lcrypto', '-lresolv']
  mysql_config --cflags
  ['-I/opt/homebrew/opt/mysql/include/mysql']
  ext_options:
    library_dirs: ['/opt/homebrew/opt/mysql/lib']
    libraries: ['mysqlclient', 'zstd', 'resolv']
    extra_compile_args: ['-std=c99']
    extra_link_args: []
    include_dirs: ['/opt/homebrew/opt/mysql/include/mysql']
    extra_objects: []
    define_macros: [('version_info', "(2,0,3,'final',0)"), ('__version__', '2.0.3')]
  running bdist_wheel
  running build
  running build_py
  creating build
  creating build/lib.macosx-11-arm64-3.9
  creating build/lib.macosx-11-arm64-3.9/MySQLdb
  copying MySQLdb/__init__.py -> build/lib.macosx-11-arm64-3.9/MySQLdb
  copying MySQLdb/_exceptions.py -> build/lib.macosx-11-arm64-3.9/MySQLdb
  copying MySQLdb/connections.py -> build/lib.macosx-11-arm64-3.9/MySQLdb
  copying MySQLdb/converters.py -> build/lib.macosx-11-arm64-3.9/MySQLdb
  copying MySQLdb/cursors.py -> build/lib.macosx-11-arm64-3.9/MySQLdb
  copying MySQLdb/release.py -> build/lib.macosx-11-arm64-3.9/MySQLdb
  copying MySQLdb/times.py -> build/lib.macosx-11-arm64-3.9/MySQLdb
  creating build/lib.macosx-11-arm64-3.9/MySQLdb/constants
  copying MySQLdb/constants/__init__.py -> build/lib.macosx-11-arm64-3.9/MySQLdb/constants
  copying MySQLdb/constants/CLIENT.py -> build/lib.macosx-11-arm64-3.9/MySQLdb/constants
  copying MySQLdb/constants/CR.py -> build/lib.macosx-11-arm64-3.9/MySQLdb/constants
  copying MySQLdb/constants/ER.py -> build/lib.macosx-11-arm64-3.9/MySQLdb/constants
  copying MySQLdb/constants/FIELD_TYPE.py -> build/lib.macosx-11-arm64-3.9/MySQLdb/constants
  copying MySQLdb/constants/FLAG.py -> build/lib.macosx-11-arm64-3.9/MySQLdb/constants
  running build_ext
  building 'MySQLdb._mysql' extension
  creating build/temp.macosx-11-arm64-3.9
  creating build/temp.macosx-11-arm64-3.9/MySQLdb
  clang -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -Dversion_info=(2,0,3,'final',0) -D__version__=2.0.3 -I/opt/homebrew/opt/mysql/include/mysql -I/opt/homebrew/opt/python@3.9/Frameworks/Python.framework/Versions/3.9/include/python3.9 -c MySQLdb/_mysql.c -o build/temp.macosx-11-arm64-3.9/MySQLdb/_mysql.o -std=c99
  clang -bundle -undefined dynamic_lookup -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk build/temp.macosx-11-arm64-3.9/MySQLdb/_mysql.o -L/opt/homebrew/opt/mysql/lib -lmysqlclient -lzstd -lresolv -o build/lib.macosx-11-arm64-3.9/MySQLdb/_mysql.cpython-39-darwin.so
  ld: library not found for -lzstd
  clang: error: linker command failed with exit code 1 (use -v to see invocation)
  error: command '/usr/bin/clang' failed with exit code 1
  ----------------------------------------
  ERROR: Failed building wheel for mysqlclient
  Running setup.py clean for mysqlclient
Failed to build mysqlclient
Installing collected packages: mysqlclient
    Running setup.py install for mysqlclient ... error
    ERROR: Command errored out with exit status 1:
     command: /opt/homebrew/opt/python@3.9/bin/python3.9 -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/tmp/pip-install-fa9eyul5/mysqlclient_31eb227649674b20a8b353f244cce7a6/setup.py'"'"'; __file__='"'"'/private/tmp/pip-install-fa9eyul5/mysqlclient_31eb227649674b20a8b353f244cce7a6/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /private/tmp/pip-record-obre5n1f/install-record.txt --single-version-externally-managed --compile --install-headers /opt/homebrew/opt/python@3.9/Frameworks/Python.framework/Versions/3.9/include/python3.9/mysqlclient
         cwd: /private/tmp/pip-install-fa9eyul5/mysqlclient_31eb227649674b20a8b353f244cce7a6/
    Complete output (43 lines):
    mysql_config --version
    ['8.0.26']
    mysql_config --libs
    ['-L/opt/homebrew/opt/mysql/lib', '-lmysqlclient', '-lz', '-lzstd', '-lssl', '-lcrypto', '-lresolv']
    mysql_config --cflags
    ['-I/opt/homebrew/opt/mysql/include/mysql']
    ext_options:
      library_dirs: ['/opt/homebrew/opt/mysql/lib']
      libraries: ['mysqlclient', 'zstd', 'resolv']
      extra_compile_args: ['-std=c99']
      extra_link_args: []
      include_dirs: ['/opt/homebrew/opt/mysql/include/mysql']
      extra_objects: []
      define_macros: [('version_info', "(2,0,3,'final',0)"), ('__version__', '2.0.3')]
    running install
    running build
    running build_py
    creating build
    creating build/lib.macosx-11-arm64-3.9
    creating build/lib.macosx-11-arm64-3.9/MySQLdb
    copying MySQLdb/__init__.py -> build/lib.macosx-11-arm64-3.9/MySQLdb
    copying MySQLdb/_exceptions.py -> build/lib.macosx-11-arm64-3.9/MySQLdb
    copying MySQLdb/connections.py -> build/lib.macosx-11-arm64-3.9/MySQLdb
    copying MySQLdb/converters.py -> build/lib.macosx-11-arm64-3.9/MySQLdb
    copying MySQLdb/cursors.py -> build/lib.macosx-11-arm64-3.9/MySQLdb
    copying MySQLdb/release.py -> build/lib.macosx-11-arm64-3.9/MySQLdb
    copying MySQLdb/times.py -> build/lib.macosx-11-arm64-3.9/MySQLdb
    creating build/lib.macosx-11-arm64-3.9/MySQLdb/constants
    copying MySQLdb/constants/__init__.py -> build/lib.macosx-11-arm64-3.9/MySQLdb/constants
    copying MySQLdb/constants/CLIENT.py -> build/lib.macosx-11-arm64-3.9/MySQLdb/constants
    copying MySQLdb/constants/CR.py -> build/lib.macosx-11-arm64-3.9/MySQLdb/constants
    copying MySQLdb/constants/ER.py -> build/lib.macosx-11-arm64-3.9/MySQLdb/constants
    copying MySQLdb/constants/FIELD_TYPE.py -> build/lib.macosx-11-arm64-3.9/MySQLdb/constants
    copying MySQLdb/constants/FLAG.py -> build/lib.macosx-11-arm64-3.9/MySQLdb/constants
    running build_ext
    building 'MySQLdb._mysql' extension
    creating build/temp.macosx-11-arm64-3.9
    creating build/temp.macosx-11-arm64-3.9/MySQLdb
    clang -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -Dversion_info=(2,0,3,'final',0) -D__version__=2.0.3 -I/opt/homebrew/opt/mysql/include/mysql -I/opt/homebrew/opt/python@3.9/Frameworks/Python.framework/Versions/3.9/include/python3.9 -c MySQLdb/_mysql.c -o build/temp.macosx-11-arm64-3.9/MySQLdb/_mysql.o -std=c99
    clang -bundle -undefined dynamic_lookup -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk build/temp.macosx-11-arm64-3.9/MySQLdb/_mysql.o -L/opt/homebrew/opt/mysql/lib -lmysqlclient -lzstd -lresolv -o build/lib.macosx-11-arm64-3.9/MySQLdb/_mysql.cpython-39-darwin.so
    ld: library not found for -lzstd
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
    error: command '/usr/bin/clang' failed with exit code 1
    ----------------------------------------
ERROR: Command errored out with exit status 1: /opt/homebrew/opt/python@3.9/bin/python3.9 -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/tmp/pip-install-fa9eyul5/mysqlclient_31eb227649674b20a8b353f244cce7a6/setup.py'"'"'; __file__='"'"'/private/tmp/pip-install-fa9eyul5/mysqlclient_31eb227649674b20a8b353f244cce7a6/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /private/tmp/pip-record-obre5n1f/install-record.txt --single-version-externally-managed --compile --install-headers /opt/homebrew/opt/python@3.9/Frameworks/Python.framework/Versions/3.9/include/python3.9/mysqlclient Check the logs for full command output.

解决办法:


编辑配置文件:


open -e .bash_profile

加入如下配置:
export PATH="/opt/homebrew/opt/openssl@1.1/bin:$PATH"
export LDFLAGS="-L/opt/homebrew/opt/openssl@1.1/lib"
export CPPFLAGS="-I/opt/homebrew/opt/openssl@1.1/include"
export PKG_CONFIG_PATH="/opt/homebrew/opt/openssl@1.1/lib/pkgconfig"
export PATH="/opt/homebrew/opt/mysql/bin:$PATH"

#添加到gcc头文件
export C_INCLUDE_PATH=$C_INCLUDE_PATH:/opt/homebrew/Cellar/zstd/1.5.0/lib

#添加到g++头文件路径
export CPLUS_INCLUDE_PATH=$CPLUS_INCLUDE_PATH:/opt/homebrew/Cellar/zstd/1.5.0/lib

#添加到动态库
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/homebrew/Cellar/zstd/1.5.0/lib

#添加到静态库
export LIBRARY_PATH=$LIBRARY_PATH:/opt/homebrew/Cellar/zstd/1.5.0/lib

重新编译:



source .bash_profile

再次执行安装命令:



env LDFLAGS="-I/opt/homebrew/opt/openssl@1.1/include -L/opt/homebrew/opt/openssl@1.1/lib" pip --no-cache install mysqlclient

成功!



Collecting mysqlclient
  Downloading mysqlclient-2.0.3.tar.gz (88 kB)
     |████████████████████████████████| 88 kB 170 kB/s 
Building wheels for collected packages: mysqlclient
  Building wheel for mysqlclient (setup.py) ... done
  Created wheel for mysqlclient: filename=mysqlclient-2.0.3-cp39-cp39-macosx_11_0_arm64.whl size=57036 sha256=84db9dd59b430113391ca2aeece2dde1fbe8b36d92c64e5ea5c5b386ad16ad28
  Stored in directory: /private/var/folders/z1/y723tjj97656xgpf3s_3npw00000gn/T/pip-ephem-wheel-cache-927so56x/wheels/43/55/d9/a2243d4b624c18c5cba30bf88e0521147498368068cb302532
Successfully built mysqlclient
Installing collected packages: mysqlclient
Successfully installed mysqlclient-2.0.3