You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

pip安装tokenizers==0.12.1失败:Python3.6.15与Rust1.72.0兼容问题

安装tokenizers==0.12.1编译错误的解决办法

问题背景

运行 pip install tokenizers==0.12.1 安装指定版本失败,环境配置:

  • Python 3.6.15
  • Rust 1.72.0

更换Python、Rust版本后问题依旧,编译时出现可变警告和类型转换错误,报错详情:

warning: variable does not need to be mutable
--> tokenizers-lib\src\models\unigram\model.rs:265:21
|
265 | let mut target_node = &mut best_path_ends_at[key_pos];
| ----^^^^^^^^^^^
| |
| help: remove this mut
...
error: casting &T to &mut T is undefined behavior, even if the reference is unused, consider instead using an UnsafeCell
--> tokenizers-lib\src\models\bpe\trainer.rs:526:47
|
522 | let w = &words[*i] as *const _ as *mut _;
| -------------------------------- casting happened here
...
526 | let word: &mut Word = &mut (*w);
| ^^^^^^^^^
|
= note: #[deny(invalid_reference_casting)] on by default

warning: tokenizers (lib) generated 3 warnings
error: could not compile tokenizers (lib) due to the previous error; 3 warnings emitted

已手动移除多余mut处理警告,但类型转换问题无法解决,需要可行的安装方案。


解决方案

1. 降级Rust到兼容版本

tokenizers==0.12.1的代码在Rust 1.64+版本中会触发严格的编译检查,降级到1.63.0及以下版本即可绕过:

  • 安装指定Rust版本:rustup install 1.63.0
  • 为当前项目设置该版本:rustup override set 1.63.0
  • 重新执行安装:pip install tokenizers==0.12.1

2. 直接安装预编译wheel包

跳过源码编译,下载对应系统和Python版本的预编译包:

  • 找到适配Python3.6的wheel文件(例如Windows系统对应tokenizers-0.12.1-cp36-none-win_amd64.whl,Linux/macOS对应对应架构的包)
  • 本地安装:pip install 下载的wheel文件名.whl

3. 修改源码编译(仅作备选)

如果必须使用当前Rust版本,需修改源码中的不安全转换:

  1. 克隆仓库并切换到指定版本:
    git clone https://github.com/huggingface/tokenizers.git
    cd tokenizers
    git checkout v0.12.1
    
  2. 打开tokenizers-lib/src/models/bpe/trainer.rs,将第522-526行的不安全转换替换为UnsafeCell实现:
    // 替换原代码
    use std::cell::UnsafeCell;
    let word = unsafe { &mut *UnsafeCell::raw_get(&words[*i] as *const _ as *mut _) };
    
  3. 本地编译安装:pip install .

内容的提问来源于stack exchange,提问作者AhmetTasdemir

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 21:13:12