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

已安装并导入torch,导入m3inference时仍报torch未定义错误如何解决?

解决m3inference导入时的NameError: name 'torch' is not defined问题

问题描述

我执行了以下操作安装并导入m3inference:

pip install m3inference
import m3inference

但出现了如下错误:

NameError                                 Traceback (most recent call last)
<ipython-input-9-50ee37ff85fa> in <module>
----> 1 import m3inference

3 frames
/usr/local/lib/python3.8/dist-packages/m3inference/full_model.py in M3InferenceModel()
     10 
     11 class M3InferenceModel(nn.Module):
---> 12     def __init__(self, device='cuda' if torch.cuda.is_available() else 'cpu'):
     13         super(M3InferenceModel, self).__init__()
     14 

NameError: name 'torch' is not defined

注:我在安装并导入m3inference之前,已经安装并导入了torch。

解决方案

  • 卸载现有m3inference:
    pip uninstall -y m3inference
    
  • 重新安装m3inference,强制让pip重新处理依赖关系:
    pip install --force-reinstall m3inference
    
  • 如果上述方法无效,检查torch版本是否与m3inference兼容,安装m3inference官方推荐的torch版本后再重新安装m3inference。
  • 不推荐但可临时解决的方法:找到/usr/local/lib/python3.8/dist-packages/m3inference/full_model.py文件,在文件开头添加以下导入语句:
    import torch
    import torch.nn as nn
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 15:35:15