如何解决从Git仓库安装detectron2时依赖torch的安装报错问题?
问题:Streamlit部署detectron2时requirements.txt安装失败
尝试部署依赖detectron2的Streamlit应用,本地通过python -m pip install 'git+https://github.com/facebookresearch/detectron2.git'安装detectron2时,必须预先安装torch,否则会直接报错。执行pip freeze > requirements.txt生成的依赖文件内容如下:
torch==1.12.1 torchaudio==0.12.1 torchvision==0.13.1 detectron2 @ git+https://github.com/facebookresearch/detectron2.git@717ab9f0aeca216a2f800e43d705766251ba3a55
但运行pip install -r requirements.txt时触发报错,核心错误是detectron2的setup.py在生成元数据阶段找不到torch模块,具体报错信息如下:
Collecting detectron2@ git+https://github.com/facebookresearch/detectron2.git@717ab9f0aeca216a2f800e43d705766251ba3a55 Cloning https://github.com/facebookresearch/detectron2.git (to revision 717ab9f0aeca216a2f800e43d705766251ba3a55) to /tmp/pip-install-60odo0ml/detectron2_3c0ef1b35e7b42cd95cfd938e09dd4d5 Running command git clone --filter=blob:none --quiet https://github.com/facebookresearch/detectron2.git /tmp/pip-install-60odo0ml/detectron2_3c0ef1b35e7b42cd95cfd938e09dd4d5 Running command git rev-parse -q --verify 'sha^717ab9f0aeca216a2f800e43d705766251ba3a55' Running command git fetch -q https://github.com/facebookresearch/detectron2.git 717ab9f0aeca216a2f800e43d705766251ba3a55 Resolved https://github.com/facebookresearch/detectron2.git to commit 717ab9f0aeca216a2f800e43d705766251ba3a55 Preparing metadata (setup.py) ... error error: subprocess-exited-with-error × python setup.py egg_info did not run successfully. │ exit code: 1 ╰─> [6 lines of output] Traceback (most recent call last): File "<string>", line 36, in <module> File "<pip-setuptools-caller>", line 34, in <module> File "/tmp/pip-install-60odo0ml/detectron2_3c0ef1b35e7b42cd95cfd938e09dd4d5/setup.py", line 10, in <module> import torch ModuleNotFoundError: No module named 'torch' [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. error: metadata-generation-failed × Encountered error while generating package metadata. ╰─> See above for output. note: This is an issue with the package mentioned above, not pip. hint: See above for details.
解决方法
方法1:分阶段安装依赖
pip处理requirements.txt时不会严格按顺序安装,尤其是git源包会优先处理元数据生成,导致torch还未安装就先处理detectron2。可以拆分安装步骤:
- 先安装torch相关依赖:
pip install torch==1.12.1 torchaudio==0.12.1 torchvision==0.13.1 - 再安装指定版本的detectron2:
pip install 'git+https://github.com/facebookresearch/detectron2.git@717ab9f0aeca216a2f800e43d705766251ba3a55'
在Streamlit部署配置中,把这两个命令按顺序添加到部署脚本即可,替代一次性安装requirements.txt。
方法2:拆分requirements文件
将依赖分成两个文件,强制先安装torch再安装detectron2:
- 创建
torch-reqs.txt:torch==1.12.1 torchaudio==0.12.1 torchvision==0.13.1 - 创建
detectron2-reqs.txt:detectron2 @ git+https://github.com/facebookresearch/detectron2.git@717ab9f0aeca216a2f800e43d705766251ba3a55 --no-deps
然后执行:
pip install -r torch-reqs.txt && pip install -r detectron2-reqs.txt
添加--no-deps是避免detectron2的setup.py尝试处理依赖,确保torch已提前安装完成。
方法3:使用预编译wheel包(推荐部署使用)
detectron2提供对应torch版本的预编译wheel包,无需源码编译,也就不会触发setup.py中的torch导入操作:
- 确认你的torch版本和CUDA版本(比如torch 1.12.1+cu113),使用对应链接安装:
pip install detectron2 -f https://dl.fbaipublicfiles.com/detectron2/wheels/cu113/torch1.12/index.html - 修改requirements.txt,加入find-links指定wheel源:
torch==1.12.1 torchaudio==0.12.1 torchvision==0.13.1 detectron2 --find-links https://dl.fbaipublicfiles.com/detectron2/wheels/cu113/torch1.12/index.html
这样pip会先安装torch,再从指定链接拉取预编译的detectron2包,跳过源码编译步骤。
内容的提问来源于stack exchange,提问作者Deshwal
相关产品推荐
相关产品推荐

