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

已安装TensorFlow与PyTorch,调用transformers仍触发RuntimeError报错

Transformers库已安装PyTorch/TensorFlow仍报错的解决方法

问题描述

我在使用Python的transformers库时遇到以下报错:

RuntimeError: At least one of TensorFlow 2.0 or PyTorch should be installed. To install TensorFlow 2.0, read the instructions at https://www.tensorflow.org/install/ To install PyTorch, read the instructions at https://pytorch.org/

已通过pip完成以下库的安装,重新安装后仍出现相同报错:

  • PyTorch:版本2.0.1+cpu
  • TensorFlow:版本2.12.0
  • transformers:版本4.29.2

解决方法

  • 检查环境一致性:确认运行代码的Python环境和安装库的pip所属环境一致。Linux/macOS可运行which python和which pip,Windows运行where python和where pip查看路径是否匹配;使用虚拟环境的话,确保已激活对应环境。
  • 验证库可用性:在代码开头添加测试代码,确认PyTorch和TensorFlow能正常导入:
    import torch
    import tensorflow as tf
    print(torch.__version__)
    print(tf.__version__)
    
    若导入失败,说明安装存在问题,需卸载后重新匹配Python版本和系统架构安装。
  • 降级transformers版本:新版本transformers可能与旧版PyTorch/TensorFlow存在兼容性问题,可尝试安装兼容版本:
    pip install transformers==4.28.0
    
  • 强制环境安装:若存在多Python环境冲突,使用python -m pip install [库名]强制在当前运行的Python环境中安装或更新库。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 01:35:06