【BUG记录】解决安装PyTorch3D时出现的“No module named ‘torch‘“错误

解决PyTorch3D安装报错:ModuleNotFoundError: No module named 'torch'

在复现3D视觉相关项目时,我遇到了一个看似简单却棘手的PyTorch3D安装问题------明明环境中已安装PyTorch,却在安装PyTorch3D时提示找不到torch模块。本文将完整记录问题现象、排查过程和最终解决方案,希望能帮到遇到同类问题的开发者。

一、问题现象

项目依赖文件requirements.txt中包含PyTorch3D的安装配置:

复制代码
git+https://github.com/facebookresearch/pytorch3d.git@stable

执行pip install -r requirements.txt时,安装过程中出现以下核心报错:

复制代码
ModuleNotFoundError: No module named 'torch'

完整报错日志片段如下:

python 复制代码
Collecting git+https://github.com/facebookresearch/pytorch3d.git@stable (from -r requirements.txt (line 24))
  Cloning https://github.com/facebookresearch/pytorch3d.git (to revision stable) to /tmp/pip-req-build-7_r229l1
  Running command git clone --filter=blob:none --quiet https://github.com/facebookresearch/pytorch3d.git /tmp/pip-req-build-7_r229l1
  Running command git checkout -q 75ebeeaea0908c5527e7b1e305fbc7681382db47
  Resolved https://github.com/facebookresearch/pytorch3d.git to commit 75ebeeaea0908c5527e7b1e305fbc7681382db47
  Installing build dependencies ... done
  Getting requirements to build wheel ... error
  error: subprocess-exited-with-error
  
  × Getting requirements to build wheel did not run successfully.
  │ exit code: 1
  ╰─> [17 lines of output]
      Traceback (most recent call last):
        # 省略部分栈信息
        File "<string>", line 15, in <module>
      ModuleNotFoundError: No module named 'torch'
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
ERROR: Failed to build 'git+https://github.com/facebookresearch/pytorch3d.git@stable' when getting requirements to build wheel

初步排查

  1. 确认环境中已安装PyTorch:执行python -c "import torch; print(torch.__version__)",能正常输出PyTorch版本,说明torch模块本身存在;
  2. 尝试单独安装PyTorch3D(pip install git+https://github.com/facebookresearch/pytorch3d.git@stable),依然报相同错误;
  3. 检查Python环境路径,确认pip和python命令指向同一虚拟环境,无环境混淆问题。

二、问题根源分析

经过查阅PyTorch3D官方issues和社区讨论,发现该问题并非PyTorch未安装,而是新版pip/setuptools/wheel工具链与PyTorch3D的构建流程不兼容

  • 新版pip(25.x+)在构建第三方包时,会创建隔离的构建环境,导致无法识别当前环境已安装的PyTorch;
  • setuptools和wheel的新版本也存在类似的构建环境隔离或依赖解析逻辑变更,与PyTorch3D的setup.py脚本不兼容。

三、解决方案

通过降级pip、setuptools和wheel到兼容版本,即可解决该问题。

步骤1:降级工具链

执行以下命令安装指定版本的pip、setuptools和wheel:

bash 复制代码
pip install -U "pip<25" "setuptools==68.2.2" "wheel<0.43"

步骤2:重新安装PyTorch3D

工具链降级完成后,重新执行依赖安装命令:

bash 复制代码
pip install -r requirements.txt
# 或单独安装PyTorch3D
# pip install git+https://github.com/facebookresearch/pytorch3d.git@stable

此时能正常识别torch模块,PyTorch3D可顺利完成编译和安装。

四、版本说明

  • pip:限制版本小于25(推荐24.x版本),避免新版隔离构建环境的问题;
  • setuptools:固定为68.2.2版本,该版本与PyTorch3D的setup.py解析逻辑兼容;
  • wheel:限制版本小于0.43,配合上述工具版本保证构建流程稳定。

五、总结

  1. 当遇到"明明已安装模块却提示找不到"的安装报错时,除了检查环境路径,还需考虑工具链版本兼容性问题;
  2. PyTorch3D等需要编译的第三方库,对pip/setuptools/wheel版本敏感,新版工具链可能因逻辑变更导致构建失败;
  3. 本次问题的核心解决方案是降级工具链到兼容版本:pip install -U "pip<25" "setuptools==68.2.2" "wheel<0.43"

如果本文对你有帮助,欢迎点赞收藏~也欢迎在评论区交流更多PyTorch3D安装的踩坑经验。

相关推荐
robinson198813 小时前
崖山数据库-谓词没提前过滤优化器BUG
bug·优化器·崖山
我敲!14 小时前
Qt中用//进行中文注释可能导致意外的BUG
qt·bug
ZPC821015 小时前
MoveIt Servo 控制真实机械臂
人工智能·pytorch·算法·性能优化·机器人
Yao.Li15 小时前
PVN3D 训练与评估代码流程详解
人工智能·3d
云飞云共享云桌面16 小时前
8人SolidWorks研发共享一台服务器——性能算力共享智能按需分配
运维·服务器·网络·数据库·3d·电脑
Web极客码16 小时前
PyTorch 实战:为神经网络开启“实时自愈”模式
人工智能·pytorch·神经网络
Aevget18 小时前
CAD格式转换引擎HOOPS Exchange v2026.2.0:计算镶嵌能力升级,释放3D数据处理新潜力
3d·hoops·hoops exchange·cad格式转换·数据格式转化
weiwei228441 天前
torch.nn神经网络详解
pytorch·mnist·手写识别
rebekk1 天前
PyTorch Dispatcher介绍
人工智能·pytorch·python
2301_808414381 天前
测试中BUG的认识
bug