已安装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能正常导入:
若导入失败,说明安装存在问题,需卸载后重新匹配Python版本和系统架构安装。import torch import tensorflow as tf print(torch.__version__) print(tf.__version__) - 降级transformers版本:新版本transformers可能与旧版PyTorch/TensorFlow存在兼容性问题,可尝试安装兼容版本:
pip install transformers==4.28.0 - 强制环境安装:若存在多Python环境冲突,使用
python -m pip install [库名]强制在当前运行的Python环境中安装或更新库。
内容的提问来源于stack exchange,提问作者jenototh
相关产品推荐
相关产品推荐

