MacOS安装plyvel报错“command '/usr/bin/clang' failed with exit code 1”

根据之前MacOS安装plyvel的方法,已经homebrew安装好了leveldb,现在安装plyvel总是出错。报错“command '/usr/bin/clang' failed with exit code 1”。

$ pip3 install plyvel

Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
Collecting plyvel
  Downloading https://mirrors.aliyun.com/pypi/packages/4b/b0/23f0cc21c943355fc5d49f1933e97812269d7a865daef5b9758b62902d46/plyvel-1.5.1.tar.gz (200 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 200.1/200.1 kB 3.0 MB/s eta 0:00:00
  Installing build dependencies ... done
  Getting requirements to build wheel ... done
  Preparing metadata (pyproject.toml) ... done
Building wheels for collected packages: plyvel
  Building wheel for plyvel (pyproject.toml) ... error
  error: subprocess-exited-with-error
  
  × Building wheel for plyvel (pyproject.toml) did not run successfully.
  │ exit code: 1
  ╰─> [19 lines of output]
      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/plyvel
      copying plyvel/_version.py -> build/lib.macosx-10.9-universal2-cpython-311/plyvel
      copying plyvel/__init__.py -> build/lib.macosx-10.9-universal2-cpython-311/plyvel
      running build_ext
      building 'plyvel._plyvel' extension
      creating build/temp.macosx-10.9-universal2-cpython-311
      creating build/temp.macosx-10.9-universal2-cpython-311/plyvel
      clang -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -arch arm64 -arch x86_64 -g -I/Library/Frameworks/Python.framework/Versions/3.11/include/python3.11 -c plyvel/_plyvel.cpp -o build/temp.macosx-10.9-universal2-cpython-311/plyvel/_plyvel.o -Wall -g -x c++ -std=c++11 -stdlib=libc++
      clang: warning: '-x c++' after last input file has no effect [-Wunused-command-line-argument]
      plyvel/_plyvel.cpp:1227:10: fatal error: 'leveldb/db.h' file not found
      #include "leveldb/db.h"
               ^~~~~~~~~~~~~~
      1 error generated.
      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 plyvel
Failed to build plyvel
ERROR: Could not build wheels for plyvel, which is required to install pyproject.toml-based projects
[notice] A new release of pip is available: 23.1.2 -> 23.3.2
[notice] To update, run: pip3 install --upgrade pip


喜欢这个问题 | 分享 | 新建回答

回答

妹岛

Jan 27, 2024
2 赞

你这个错误的本质是“fatal error: 'leveldb/db.h' file not found




首先你需要保证你安装了Apple的命令行工具,可能你已经安装了,无妨,运行一下如下代码就可以知道是否安装了:

xcode-select --install

如果安装了就会告诉你安装了,如果没安装,就会帮你安装。



如果依然报错,我相信,问题的核心根本是"Mac M1/M2"这类苹果芯片的MacOS系统,在安装plyvel时报错。

Mac M1/M2/M3 安装 plyvel 报错的解决方案如下:

在系统环境变量描述文件中添加如下内容,Mac M1/M2/M3 这些的系统环境变量描述应该已经不是bashrc了而是~/.zshrc了,"sudo vim ~/.zshrc"即可:

export LIBRARY_PATH="$LIBRARY_PATH:$(brew --prefix)/lib"
export CPATH="$CPATH:$(brew --prefix)/include"

加上上面两行后,然后关闭终端,重新打开终端,运行pip命令安装plyvel即可。