本地Python Notebook导入transformers/pipeline遇RuntimeError报错求助
问题解决:transformers pipeline 导入报错
module 'tensorflow' has no attribute 'data' 问题重现
执行代码:
from transformers import pipeline # Load the text summarization pipeline summarizer = pipeline("summarization")
触发报错:
RuntimeError: Failed to import transformers.pipelines because of the following error (look up to see its traceback): module 'tensorflow' has no attribute 'data'
核心原因
这个报错本质是TensorFlow版本不兼容或安装不完整:tf.data是TensorFlow 2.x的核心模块,若安装的是TensorFlow 1.x版本,或者TF 2.x安装损坏,都会出现该问题;另外也可能是transformers库与当前TF版本的适配性问题。
解决方案
1. 升级/重装TensorFlow到2.x版本
卸载旧版本并安装最新稳定版:
pip uninstall tensorflow -y pip install tensorflow
若需指定兼容版本(避免新版本适配问题),可安装2.15.0(当前稳定LTS版本):
pip install tensorflow==2.15.0
2. 更新transformers库到最新版
旧版transformers对TF 2.x的支持不完善,执行更新:
pip install --upgrade transformers
3. 验证TensorFlow安装完整性
在Notebook中单独运行以下代码,确认TF是否正常:
import tensorflow as tf print(tf.data)
若仍报错,说明TF安装不完整,建议用conda重新安装(若使用conda环境):
conda uninstall tensorflow -y conda install tensorflow
4. 切换到PyTorch后端(绕开TF问题)
transformers支持双后端,若不想折腾TF,可直接指定使用PyTorch:
from transformers import pipeline # 指定framework="pt"使用PyTorch,需先安装PyTorch summarizer = pipeline("summarization", framework="pt")
未安装PyTorch的话先执行:
pip install torch torchvision torchaudio
内容的提问来源于stack exchange,提问作者Jesvi Jonathan
相关产品推荐
相关产品推荐

