ERROR: Failed building wheel for lxml
问题在使用 pip install lxml
或者依赖包含 lxml
的项目时,常常会遇到如下错误:
ERROR: Failed building wheel for lxml
ERROR: Command errored out with exit status 1:
command: /usr/bin/python3 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-xxxx/lxml/setup.py'"'"'; __file__='"'"'/tmp/pip-install-xxxx/lxml/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d /tmp/pip-wheel-xxxx
cwd: /tmp/pip-install-xxxx/lxml/
Complete output (… lines omitted …):
**snip**
lxml/includes/etreepublic.pxi:1:10: fatal error: libxml/xmlversion.h: No such file or directory
1 | #include "libxml/xmlversion.h"
| ^~~~~~~~~~~~~~~~~~~~~
compilation terminated.
error: command '/usr/bin/cc' failed with exit code 1
----------------------------------------
ERROR: Failed building wheel for lxml
Running setup.py clean for lxml
该错误核心在于在本地编译 lxml
的 C 扩展模块时找不到所需的底层依赖或编译工具链,导致无法生成对应的 wheel 包。以下将从以下几个方面,结合最常见的平台环境,给出超详细的解决方案。
libxml2
与 libxslt
。.whl
),以避免用户在本地编译源代码。sdist
)编译安装,这就需要系统中已有 C 编译器以及开发头文件和库文件。libxml/xmlversion.h
,即没有安装 libxml2-dev
(或 Windows 下的等效 SDK)。# 更新包列表
sudo apt update
# 安装 C 编译器和开发头文件
sudo apt install -y build-essential python3-dev \
libxml2-dev libxslt1-dev zlib1g-dev
# 安装 wheel 工具
pip install --upgrade pip setuptools wheel
# 重新安装 lxml
pip install lxml
说明
build-essential
:包含 GCC、make 等常用编译工具python3-dev
:Python 头文件libxml2-dev
、libxslt1-dev
:lxml 源码编译所需Alpine Linux 使用 apk
包管理,且默认使用 musl libc,需要:
# 安装编译工具与依赖
apk update
apk add --no-cache build-base python3-dev \
libxml2-dev libxslt-dev
# 安装 wheel 并编译
pip install --upgrade pip setuptools wheel
pip install lxml
macOS 环境下可以借助 Homebrew 安装依赖:
# 安装 Homebrew(若尚未安装):
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# 安装 libxml2 与 libxslt
brew install libxml2 libxslt
# 指定 pkg-config 路径(若编译仍失败)
export PATH="/usr/local/opt/libxml2/bin:$PATH"
export LDFLAGS="-L/usr/local/opt/libxml2/lib"
export CPPFLAGS="-I/usr/local/opt/libxml2/include"
# 安装 wheel
pip install --upgrade pip setuptools wheel
# 安装 lxml
pip install lxml
Windows 下推荐使用官方预编译 wheel 或安装 Build Tools:
安装 Visual C++ Build Tools
或直接下载预编译 wheel
访问 Unofficial Windows Binaries,下载与 Python 版本、位数相符的 lxml-…-cp3x-…-win_amd64.whl
文件。
本地安装:
pip install path\to\lxml-4.x.x-cp39-cp39-win_amd64.whl
使用 pip 升级到最新
pip install --upgrade pip setuptools wheel
直接安装最新版本
pip install lxml==4.9.2
Conda 环境 如果使用 Anaconda/Miniconda,Conda 仓库通常自带预编译包:
conda install -c conda-forge lxml
有时 pip 本身版本过低或缓存包损坏也会导致问题:
# 升级 pip
pip install --upgrade pip
# 清理 pip 缓存
pip cache purge
# 重试安装
pip install lxml
ERROR: Failed building wheel for lxml
本质上是 C 扩展编译失败,通常由于缺少编译器或开发包头文件。只要根据你的操作系统正确安装:
libxml2-dev
、libxslt1-dev
、build-essential
、python3-dev
libxml2
、libxslt
,配置环境变量之后,升级 pip
/setuptools
/wheel
并重新执行 pip install lxml
,即可顺利拿到二进制 wheel 包或完成本地编译。希望这篇超详细的技术博客能帮助你彻底解决该问题!
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有