macOS Big Sur 11.3.1安装lxml 3.8.0失败报错问题求助
macOS系统安装lxml 3.8.0失败解决方案
问题复现
在空项目虚拟环境中执行安装命令:
(env) ~/Desktop/lxml-3.8.0-test$ pip install lxml==3.8.0 --log log.txt
报错信息如下:
Collecting lxml==3.8.0 Using cached https://files.pythonhosted.org/packages/20/b3/9f245de14b7696e2d2a386c0b09032a2ff6625270761d6543827e667d8de/lxml-3.8.0.tar.gz Installing collected packages: lxml Running setup.py install for lxml ... error ERROR: Command errored out with exit status 1: /Users/ceasar/Desktop/lxml-3.8.0-test/env/bin/python3 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/private/var/folders/hs/mywxk87n21q3rdbyfn24t6v80000gn/T/pip-install-r_dh7_0_/lxml/setup.py'"'"'; __file__='"'"'/private/var/folders/hs/mywxk87n21q3rdbyfn24t6v80000gn/T/pip-install-r_dh7_0_/lxml/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/hs/mywxk87n21q3rdbyfn24t6v80000gn/T/pip-record-io8knvww/install-record.txt --single-version-externally-managed --compile --install-headers /Users/ceasar/Desktop/lxml-3.8.0-test/env/include/site/python3.8/lxml Check the logs for full command output.
尝试加STATIC_DEPS=true参数强制编译依赖仍报错:
(env) ~/Desktop/lxml-3.8.0-test$ STATIC_DEPS=true sudo pip install lxml==3.8.0 --log log.txt
报错日志关键信息:
... 2021-09-16T10:07:15,323 creating build/temp.macosx-10.14.6-x86_64-3.8/src/lxml 2021-09-16T10:07:15,323 clang -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -iwithsysroot/System/Library/Frameworks/System.framework/PrivateHeaders -iwithsysroot/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.8/Headers -arch arm64 -arch x86_64 -Werror=implicit-function-declaration -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -Isrc/lxml/includes -I/Users/ceasar/Desktop/lxml-3.8.0-test/env/include -I/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/include/python3.8 -c src/lxml/lxml.etree.c -o build/temp.macosx-10.14.6-x86_64-3.8/src/lxml/lxml.etree.o -w -flat_namespace 2021-09-16T10:07:16,822 src/lxml/lxml.etree.c:233279:65: error: too many arguments to function call, expected 3, have 4 2021-09-16T10:07:16,822 return (*((__Pyx_PyCFunctionFast)meth)) (self, args, nargs, NULL); 2021-09-16T10:07:16,822 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^~~~ ... 2021-09-16T10:07:16,831 /Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/include/python3.8/cpython/pystate.h:79:15: note: 'curexc_value' declared here 2021-09-16T10:07:16,831 PyObject *curexc_value; 2021-09-16T10:07:16,831 ^ 2021-09-16T10:07:16,831 fatal error: too many errors emitted, stopping now [-ferror-limit=] 2021-09-16T10:07:16,872 20 errors generated. 2021-09-16T10:07:16,884 Compile failed: command 'clang' failed with exit status 1 2021-09-16T10:07:16,942 creating tmp 2021-09-16T10:07:16,942 cc -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include -I/usr/include/libxml2 -c /tmp/xmlXPathInituaq3ydct.c -o tmp/xmlXPathInituaq3ydct.o 2021-09-16T10:07:17,005 cc tmp/xmlXPathInituaq3ydct.o -L/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib -lxml2 -o a.out 2021-09-16T10:07:17,091 error: command 'clang' failed with exit status 1
根本原因
lxml 3.8.0发布于2017年,其内置的由旧版本Cython生成的C代码,不兼容Python 3.8及以上版本的ABI接口,too many arguments to function call就是典型的接口不匹配报错,和系统的libxml2、libxslt版本无关。
解决方案
按优先级选择以下方案即可:
- 方案1:降级Python版本(最稳定)
lxml 3.8.0官方支持的最高Python版本为3.7,可安装Python 3.7版本,用该版本创建虚拟环境,激活后直接执行pip install lxml==3.8.0即可正常安装。 - 方案2:手动适配编译(无需降级Python)
如果必须使用当前Python 3.8版本,先单独安装新版Cython:pip install cython,下载lxml 3.8.0源码包:pip download lxml==3.8.0 --no-deps,解压源码包后进入目录删除所有.c后缀的预生成文件,让Cython重新生成适配当前Python版本的C代码,最后执行python setup.py install完成安装。 - 方案3:添加编译参数临时绕过
如果只是临时安装测试,可以在安装时添加clang编译参数跳过隐式函数声明报错:
CFLAGS="-Wno-error=implicit-function-declaration" pip install lxml==3.8.0
内容的提问来源于stack exchange,提问作者Ceasar
相关产品推荐
相关产品推荐

