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

从transformers导入pipeline时出现NameError: '_C'未定义错误求助

问题描述

尝试用transformers库构建问答Pipeline,代码如下:

from transformers import pipeline
question_answerer = pipeline("question-answering", model='distilbert-base-cased-distilled-squad')

运行时触发如下错误:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-26-d4a7597b20d3> in <cell line: 1>()
----> 1 from transformers import pipeline
      2 question_answerer = pipeline("question-answering", model='distilbert-base-cased-distilled-squad')

4 frames
/usr/local/lib/python3.10/dist-packages/torch/__init__.py in <module>
    560     raise  # If __file__ is not None the cause is unknown, so just re-raise.
    561 
--> 562 for name in dir(_C):
    563     if name[0] != '_' and not name.endswith('Base'):
    564         __all__.append(name)

NameError: name '_C' is not defined
解决方法

这个错误是PyTorch核心C扩展模块(_C)加载失败导致的,通常是PyTorch安装不完整或损坏,可按以下步骤排查:

  • 完全卸载现有PyTorch后重新安装:
    先执行卸载命令:

    pip uninstall -y torch
    

    CPU环境直接执行安装命令:

    pip install torch
    

    如果使用GPU,需匹配本地CUDA版本安装对应PyTorch包。

  • 检查虚拟环境:如果使用了虚拟环境,确保激活了正确的环境,避免全局环境和虚拟环境的PyTorch版本冲突。

  • 清理pip缓存后重新安装:缓存的损坏安装包可能导致安装异常,执行以下命令清理缓存后再重新安装PyTorch:

    pip cache purge
    pip install torch
    
  • 验证安装:重新安装后,运行以下代码确认PyTorch是否正常加载:

    import torch
    print(torch.__version__)
    # 若有GPU,可检查CUDA是否可用
    print(torch.cuda.is_available())
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 08:12:33