安装 mysqlclient 失败,提示Could not build wheels for mysqlclient, which is required to install pyproject.toml-based projects 的解决方案

刚学习 Python 语言,运行一个现成的项目需要安装 mysqlclient,执行 pip install mysqlclient 之后,就一直报错,具体内容为:

Collecting mysqlclient
  Using cached mysqlclient-2.2.0.tar.gz (89 kB)
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Installing backend dependencies ... done
  Preparing metadata (pyproject.toml) ... done
Building wheels for collected packages: mysqlclient
  Building wheel for mysqlclient (pyproject.toml) ... error
  error: subprocess-exited-with-error
  
  × Building wheel for mysqlclient (pyproject.toml) did not run successfully.
   exit code: 1
  ╰─> [47 lines of output]
      Trying pkg-config --exists mysqlclient
      Command 'pkg-config --exists mysqlclient' returned non-zero exit status 1.
      Trying pkg-config --exists mariadb
      # Options for building extention module:
        extra_compile_args: ['-I/opt/homebrew/Cellar/mariadb/11.1.2/include/mysql', '-std=c99']
        extra_link_args: ['-L/opt/homebrew/Cellar/mariadb/11.1.2/lib', '-lmariadb', '-lssl', '-lcrypto']
        define_macros: [('version_info', (2, 2, 0, 'final', 0)), ('__version__', '2.2.0')]
      running bdist_wheel
      running build
      running build_py
      creating build
      creating build/lib.macosx-10.9-universal2-cpython-311
      creating build/lib.macosx-10.9-universal2-cpython-311/MySQLdb
      copying src/MySQLdb/release.py -> build/lib.macosx-10.9-universal2-cpython-311/MySQLdb
      copying src/MySQLdb/cursors.py -> build/lib.macosx-10.9-universal2-cpython-311/MySQLdb
      copying src/MySQLdb/connections.py -> build/lib.macosx-10.9-universal2-cpython-311/MySQLdb
      copying src/MySQLdb/__init__.py -> build/lib.macosx-10.9-universal2-cpython-311/MySQLdb
      copying src/MySQLdb/times.py -> build/lib.macosx-10.9-universal2-cpython-311/MySQLdb
      copying src/MySQLdb/converters.py -> build/lib.macosx-10.9-universal2-cpython-311/MySQLdb
      copying src/MySQLdb/_exceptions.py -> build/lib.macosx-10.9-universal2-cpython-311/MySQLdb
      creating build/lib.macosx-10.9-universal2-cpython-311/MySQLdb/constants
      copying src/MySQLdb/constants/FLAG.py -> build/lib.macosx-10.9-universal2-cpython-311/MySQLdb/constants
      copying src/MySQLdb/constants/CLIENT.py -> build/lib.macosx-10.9-universal2-cpython-311/MySQLdb/constants
      copying src/MySQLdb/constants/__init__.py -> build/lib.macosx-10.9-universal2-cpython-311/MySQLdb/constants
      copying src/MySQLdb/constants/ER.py -> build/lib.macosx-10.9-universal2-cpython-311/MySQLdb/constants
      copying src/MySQLdb/constants/CR.py -> build/lib.macosx-10.9-universal2-cpython-311/MySQLdb/constants
      copying src/MySQLdb/constants/FIELD_TYPE.py -> build/lib.macosx-10.9-universal2-cpython-311/MySQLdb/constants
      running egg_info
      writing src/mysqlclient.egg-info/PKG-INFO
      writing dependency_links to src/mysqlclient.egg-info/dependency_links.txt
      writing top-level names to src/mysqlclient.egg-info/top_level.txt
      reading manifest file 'src/mysqlclient.egg-info/SOURCES.txt'
      reading manifest template 'MANIFEST.in'
      adding license file 'LICENSE'
      writing manifest file 'src/mysqlclient.egg-info/SOURCES.txt'
      copying src/MySQLdb/_mysql.c -> build/lib.macosx-10.9-universal2-cpython-311/MySQLdb
      running build_ext
      building 'MySQLdb._mysql' extension
      creating build/temp.macosx-10.9-universal2-cpython-311
      creating build/temp.macosx-10.9-universal2-cpython-311/src
      creating build/temp.macosx-10.9-universal2-cpython-311/src/MySQLdb
      clang -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -arch arm64 -arch x86_64 -g -I/usr/local/opt/openssl@3/include "-Dversion_info=(2, 2, 0, 'final', 0)" -D__version__=2.2.0 -I/Library/Frameworks/Python.framework/Versions/3.11/include/python3.11 -c src/MySQLdb/_mysql.c -o build/temp.macosx-10.9-universal2-cpython-311/src/MySQLdb/_mysql.o -I/opt/homebrew/Cellar/mariadb/11.1.2/include/mysql -std=c99
      clang -bundle -undefined dynamic_lookup -arch arm64 -arch x86_64 -g -L/usr/local/opt/openssl@3/lib -I/usr/local/opt/openssl@3/include build/temp.macosx-10.9-universal2-cpython-311/src/MySQLdb/_mysql.o -o build/lib.macosx-10.9-universal2-cpython-311/MySQLdb/_mysql.cpython-311-darwin.so -L/opt/homebrew/Cellar/mariadb/11.1.2/lib -lmariadb -lssl -lcrypto
      ld: warning: directory not found for option '-L/usr/local/opt/openssl@3/lib'
      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 code 1
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
  ERROR: Failed building wheel for mysqlclient
Failed to build mysqlclient
ERROR: Could not build wheels for mysqlclient, which is required to install pyproject.toml-based projects
ShellScript

尝试了很多,最终原因是因为找不到我安装的 openssl,所以会出现 warning: directory not found for option '-L/usr/local/opt/openssl@3/lib' 之类的。

解决方案

查看本机 openssl 安装路径

我是通过 brew install openssl@3 安装的 openssl,安装完毕后,通过 brew --prefix openssl@3 命令查看到 openssl 的路径安装在 /opt/homebrew/opt/openssl@3 这里,并没有在 /usr/local/opt/openssl@3 这里。

指定 openssl 的位置

之后使用如下两条命令设置 openssl 的 lib 和 include 的路径

export LDFLAGS="-L/usr/local/opt/openssl@3/lib"

export CPPFLAGS="-I/usr/local/opt/openssl@3/include"
ShellScript

再接下来执行 pip install mysqlclient 就可以顺利安装了。

点赞

发表回复

电子邮件地址不会被公开。必填项已用 * 标注