Python 应用程序从管道部署到 Azure Web 应用程序,不包括需求包

问题描述 投票:0回答:1

我有一个 Python Flask 应用程序,我想将其部署到 Azure Web 应用程序。

由于某种原因,在构建过程中创建的 venv 没有保留已安装的软件包,我不明白为什么。 例如,requirements.txt 中包含

openai==1.12.0

构建作业打印
Successfully installed [...] openai-1.12.0 [...]
。听起来应该没问题。

然后,部署成功后,在 Web 应用程序的 Azure shell 中运行

pip list
(antenv 虚拟环境处于活动状态)以查看安装的内容并获取没有
openai
的列表。如果我列出 venv 文件夹的内容,它包含我想要安装的所有软件包...

这是构建作业的 yaml:

          - script: |
              sudo apt-get install python3.11-dev portaudio19-dev
              ls
              python -m venv antenv
              source antenv/bin/activate
              python -m pip install --upgrade pip
              pip install -r ./requirements.txt
            workingDirectory: "$(projectRoot)"
            displayName: "Install requirements"

我还尝试将

pip install
行更改为
pip install --target="./.python_packages/lib/site-packages" -r ./requirements.txt

不幸的是,这些包位于该文件夹中,但 venv 未使用。
我做错了什么吗?有什么建议吗?

python azure azure-webapps
1个回答
0
投票

您可以使用下面的

Azure Devops yaml code
来获取构建中
pip list
任务之后的
pip install
:-

我的

requirements.txt
:-

Flask==2.0.2
gunicorn
PyYAML

Azure DevOps yaml 代码:-

trigger:
- main

variables:
  azureServiceConnectionId: '23xxxxxxxxc036a5'
  webAppName: 'valleywebapp98'

  vmImageName: 'ubuntu-latest'
  environmentName: 'valleywebapp98'

  projectRoot: $(System.DefaultWorkingDirectory)
  pythonVersion: '3.10'

stages:
- stage: Build
  displayName: Build stage
  jobs:
  - job: BuildJob
    pool:
      vmImage: $(vmImageName)
    steps:
    - task: UsePythonVersion@0
      inputs:
        versionSpec: '$(pythonVersion)'
      displayName: 'Use Python $(pythonVersion)'

    - script: |
        python -m venv antenv
        source antenv/bin/activate
        python -m pip install --upgrade pip
        pip install setup
        pip install -r requirements.txt
        pip list
      workingDirectory: $(projectRoot)
      displayName: "Install requirements"

    - task: ArchiveFiles@2
      displayName: 'Archive files'
      inputs:
        rootFolderOrFile: '$(projectRoot)'
        includeRootFolder: false
        archiveType: zip
        archiveFile: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
        replaceExistingArchive: true

    - upload: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
      displayName: 'Upload package'
      artifact: drop

- stage: Deploy
  displayName: 'Deploy Web App'
  dependsOn: Build
  condition: succeeded()
  jobs:
  - deployment: DeploymentJob
    pool:
      vmImage: $(vmImageName)
    environment: $(environmentName)
    strategy:
      runOnce:
        deploy:
          steps:

          - task: UsePythonVersion@0
            inputs:
              versionSpec: '$(pythonVersion)'
            displayName: 'Use Python version'

          - task: AzureWebApp@1
            displayName: 'Deploy Azure Web App : valleywebapp98'
            inputs:
              azureSubscription: $(azureServiceConnectionId)
              appName: $(webAppName)
              package: $(Pipeline.Workspace)/drop/$(Build.BuildId).zip

输出:-

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.