Transformers 4.11.1与Python 3.11兼容性及安装报错求助
解决transformers 4.11.1安装时tokenizers编译wheel失败问题
问题描述
安装transformers 4.11.1库时遇到编译错误,核心问题是tokenizers的wheel构建失败,完整报错信息如下:
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.9/2.9 MB 10.8 MB/s eta 0:00:00 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 295.0/295.0 kB 14.0 MB/s eta 0:00:00 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 880.6/880.6 kB 19.1 MB/s eta 0:00:00 Preparing metadata (setup.py) ... done ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 212.7/212.7 kB 18.7 MB/s eta 0:00:00 Installing build dependencies ... done Getting requirements to build wheel ... done Preparing metadata (pyproject.toml) ... done error: subprocess-exited-with-error × Building wheel for tokenizers (pyproject.toml) did not run successfully. │ exit code: 1 ╰─> See above for output. note: This error originates from a subprocess, and is likely not a problem with pip. Building wheel for tokenizers (pyproject.toml) ... error ERROR: Failed building wheel for tokenizers Building wheel for sacremoses (setup.py) ... done ERROR: Could not build wheels for tokenizers, which is required to install pyproject.toml-based projects
解决方案
以下是几种可行的解决办法,按优先级排序:
安装预编译的tokenizers二进制包
直接跳过编译步骤,安装对应版本的预编译包:pip install tokenizers==0.10.3 --only-binary :all:安装完成后再执行
pip install transformers==4.11.1即可。(注:transformers 4.11.1依赖tokenizers 0.10.x版本,指定0.10.3可确保版本兼容)补全编译依赖环境
如果需要从源码编译tokenizers,需先安装系统编译工具链:- Windows:安装Visual Studio Build Tools,勾选「C++开发工具」组件
- Linux(Debian/Ubuntu):执行
sudo apt install build-essential - Linux(CentOS/RHEL):执行
sudo yum groupinstall "Development Tools" - macOS:执行
xcode-select --install安装Command Line Tools
补全依赖后重新运行pip install transformers==4.11.1
使用conda安装
conda会自动处理预编译依赖,避免编译问题:conda install -c conda-forge transformers==4.11.1
原因说明
tokenizers库由Rust语言编写,pip安装时需要编译Rust代码生成wheel包。如果系统缺少编译工具链或Rust环境,就会出现构建失败的错误。优先选择预编译包或conda安装可快速解决问题。
内容的提问来源于stack exchange,提问作者Malik
相关产品推荐
相关产品推荐

