在尝试使用pip安装google-re2的问题记录。
报错特征
× python setup.py bdist_wheel did not run successfully.
│ exit code: 1
╰─> [177 lines of output]
running bdist_wheel
running build
running build_py
creating build
creating build/lib.macosx-10.9-x86_64-cpython-39
copying re2.py -> build/lib.macosx-10.9-x86_64-cpython-39
running build_ext
building '_re2' extension
creating build/temp.macosx-10.9-x86_64-cpython-39
....
warning: alias declarations are a C++11 extension [-Wc++11-extensions]
....
error: unknown type name 'constexpr'
找到源头仓库
re2/python at abseil · google/re2 (github.com)
尝试通过路径 re2/python
进行编译安装
python setup.py build
你可以仍然会遇到类似报错,于是查看setup.py中的环境配置代码
ext_module = setuptools.Extension(
name='_re2',
sources=['_re2.cc'],
include_dirs=list(include_dirs()),
libraries=['re2'],
extra_compile_args=['-fvisibility=hidden'],
)
修改为
absl_include_path = "/opt/homebrew/Cellar/abseil/20230802.1/include"
re2_include_path = "/opt/homebrew/Cellar/re2/20230901/include"
re2_lib_dir = "/opt/homebrew/lib"
ext_module = setuptools.Extension(
name='_re2',
sources=['_re2.cc'],
include_dirs=list(include_dirs()) + [absl_include_path, re2_include_path],
libraries=['re2'],
library_dirs=[re2_lib_dir], # This should point to the directory, not the dylib file
extra_compile_args=['-fvisibility=hidden', '-std=c++17'],
)
其中三个路径absl_include_path
,re2_include_path
和re2_lib_dir
可通过以下命令进行查找,或根据对应报错信息进一步调整。
find /opt/homebrew -name "libre2"
find /usr/local -name "libre2*"
ls /usr/local/lib | grep libre2
ls /opt/homebrew/lib | grep libre2
调整好setup.py之后再执行
python setup.py build
python setup.py install
即可完成安装。