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

Azure App Service部署Flask应用:如何通过requirements.txt安装torch与Detectron2

解决Azure App Service部署Detectron2时找不到torch的问题

问题描述

我开发了一个基于Detectron2预训练模型的Flask图像分割应用,本地运行无异常,但部署至Azure App Service时,Detectron2安装失败。报错显示安装Detectron2时找不到torch模块,虽已在requirements.txt中声明torch与Detectron2依赖,且尝试更换过Python版本,仍无法解决。

相关文件与报错信息

requirements.txt内容

firebase==3.0.1
firebase-admin==5.0.3
Flask==2.0.2
imutils==0.5.4
matplotlib==3.3.4
numpy==1.21.6
opencv-python==4.5.3.56
pandas==1.1.5
Pillow==8.4.0
scikit-image==0.17.2
scikit-learn==0.21.3
webcolors==1.11.1
Werkzeug==2.0.2
gspread==5.4.0
oauth2client==4.1.3
opencv-contrib-python==4.5.3.56
tensorflow
torch

# Detectron detectron2
git+https://github.com/facebookresearch/detectron2.git

Azure日志报错信息

python: 3.7.15
Warning: An outdated version of python was detected (3.7.15). Consider updating.
Versions supported by Oryx: https://github.com/microsoft/Oryx

Python Version: /tmp/oryx/platforms/python/3.7.15/bin/python3.7
Creating directory for command manifest file if it does not exist
Removing existing manifest file
Python Virtual Environment: antenv
Creating virtual environment...
Activating virtual environment...
Running pip install...
[05:40:09+0000] Collecting git+https://github.com/facebookresearch/detectron2.git (from -r requirements.txt (line 20))
[05:40:09+0000]   Cloning https://github.com/facebookresearch/detectron2.git to /tmp/pip-req-build-3_w8g61y
  Running command git clone --quiet https://github.com/facebookresearch/detectron2.git /tmp/pip-req-build-3_w8g61y
[05:40:12+0000]   Resolved https://github.com/facebookresearch/detectron2.git to commit 1523b3e9929a06d798871eb9afc4c9f770743baf
[05:40:12+0000]   Preparing metadata (setup.py): started
[05:40:12+0000]   Preparing metadata (setup.py): finished with status '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-req-build-3_w8g61y/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.

解决方法

  • 指定兼容的torch具体版本
    Detectron2的setup.py在生成元数据时需要提前加载torch,直接写torch会让pip安装最新版,可能和Azure环境的CUDA配置冲突。根据你使用的Detectron2 commit(1523b3e),指定适配的带CUDA后缀的torch版本,替换requirements.txt中的torch行:

    torch==1.12.1+cu113
    torchvision==0.13.1+cu113
    torchaudio==0.12.1+cu113
    

    这样pip会优先安装预编译包,避免源码编译导致的依赖问题。

  • 调整requirements.txt的依赖顺序
    pip安装依赖时会按文件顺序处理,但git源的包会先克隆并尝试生成元数据,此时torch还未安装就会报错。将Detectron2的依赖行移到torch相关依赖的后面,同时固定Detectron2的commit版本:

    # 所有基础依赖...
    torch==1.12.1+cu113
    torchvision==0.13.1+cu113
    torchaudio==0.12.1+cu113
    # 最后添加Detectron2
    git+https://github.com/facebookresearch/detectron2.git@1523b3e9929a06d798871eb9afc4c9f770743baf
    
  • 使用自定义构建脚本强制安装顺序
    如果Azure的Oryx工具仍存在顺序问题,创建一个install_deps.sh脚本,手动指定安装顺序:

    pip install torch==1.12.1+cu113 torchvision==0.13.1+cu113 torchaudio==0.12.1+cu113 --extra-index-url https://download.pytorch.org/whl/cu113
    pip install -r requirements.txt
    

    在Azure门户的应用服务配置中,将启动命令设置为bash install_deps.sh,确保torch先被安装。

  • 升级Python版本
    日志提示Python 3.7.15已过时,Oryx支持3.8及以上版本,新版本的pip和虚拟环境管理更稳定。在项目根目录创建runtime.txt文件,指定Python版本:

    python-3.9.16
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 17:01:13