【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安装的踩坑经验。

相关推荐
程序猿追5 天前
那个右下角的小数字怎么“卡”住我打字——我用 HarmonyOS 自己写了一个字数限制输入框
pytorch·华为·harmonyos
闵孚龙5 天前
《PyTorch 深度修炼》Dataset 和 DataLoader:数据如何喂给模型
人工智能·pytorch·python
bryant_meng5 天前
【VAE】From Pixels to Faces: Building a VAE from Scratch
pytorch·vae·log-sigma2·重参数
探物 AI5 天前
【3D·感知】从PointNet到PointPillars:如何让自动驾驶汽车“实时“看见3D世界?
3d·自动驾驶·汽车
苏州邦恩精密5 天前
GOM三维扫描在制造中的真实价值:让“修模”从经验动作变成数据动作
人工智能·科技·机器学习·3d·自动化·制造
装不满的克莱因瓶5 天前
了解多标签图像分类方法——从Sigmoid输出到真实世界复杂视觉理解
人工智能·pytorch·python·深度学习·机器学习·分类·数据挖掘
冷小鱼5 天前
TensorFlow 2.21 进阶实战:从训练优化到生产部署的完整指南
人工智能·pytorch·python·tensorflow
YHHLAI5 天前
CSS 3D 硬核解析:四个属性手写旋转立方体
前端·css·3d
冷小鱼5 天前
PyTorch 2.12 完全指南:从动态图到编译优化的深度学习框架演进
人工智能·pytorch·深度学习
IRevers5 天前
【大模型】Gemma4在ROCm和vLLM部署
人工智能·pytorch·深度学习·大模型·datawhale·vllm·amdev