找不到Python依赖项的Azure函数

2024-05-09 01:00:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个函数,我想使用如下所述的连续交付将其提升到Azure函数:

https://docs.microsoft.com/en-us/azure/azure-functions/functions-how-to-azure-devops?tabs=python

启动函数时,出现以下错误:

Result: Failure Exception: ModuleNotFoundError: No module named 'azure.keyvault'. Troubleshooting Guide: https://aka.ms/functions-modulenotfound Stack: File "/azure-functions-host/workers/python/3.8/LINUX/X64/azure_functions_worker/dispatcher.py", line 284, in _handle__function_load_request func = loader.load_function( File "/azure-functions-host/workers/python/3.8/LINUX/X64/azure_functions_worker/utils/wrappers.py", line 42, in call raise extend_exception_message(e, message) File "/azure-functions-host/workers/python/3.8/LINUX/X64/azure_functions_worker/utils/wrappers.py", line 40, in call return func(*args, **kwargs) File "/azure-functions-host/workers/python/3.8/LINUX/X64/azure_functions_worker/loader.py", line 76, in load_function mod = importlib.import_module(fullmodname) File "/usr/local/lib/python3.8/importlib/__init__.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "/home/site/wwwroot/CaseDB_Functions/__init__.py", line 4, in <module> from .case_crawler.federal_cases import FederalCases File "/home/site/wwwroot/CaseDB_Functions/case_crawler/federal_cases.py", line 1, in <module> from shared_code import blob_handler File "/home/site/wwwroot/shared_code/blob_handler.py", line 5, in <module> from azure.keyvault.secrets import SecretClient

(注意:仅此依赖项并不是一个特定的问题,它只是在我的代码中导入的第一个依赖项-已经用不同的包切换了第一个导入,这基本上导致了相同的错误,只是另一个包)

我已经在不同的环境中尝试过我的代码(也是Ubuntu,它应该与Azure函数使用的图像相同),并且总是使用venv。我的代码在本地总是运行良好,所以在构建代码并将其发布到Azure(通过Azure管道完成)时,我似乎遗漏了一些东西

My requirements.txt:

appdirs==1.4.4
astroid==2.5.1
azure-common==1.1.26
azure-core==1.12.0
azure-cosmos==4.2.0
azure-functions==1.6.0
azure-identity==1.5.0
azure-keyvault==4.1.0
azure-keyvault-certificates==4.2.1
azure-keyvault-keys==4.3.1
azure-keyvault-secrets==4.2.0
azure-storage-blob==12.8.0
beautifulsoup4==4.9.3
certifi==2020.12.5
cffi==1.14.5
chardet==3.0.4
colorama==0.4.4
cryptography==3.4.6
decorator==4.4.2
distlib==0.3.1
filelock==3.0.12
idna==2.10
isodate==0.6.0
isort==5.7.0
lazy-object-proxy==1.5.2
lxml==4.6.2
mccabe==0.6.1
msal==1.10.0
msrest==0.6.21
oauthlib==3.1.0
pipenv==2020.11.15
pycparser==2.20
PyJWT==2.0.1
pylint==2.7.2
requests==2.25.1
requests-oauthlib==1.3.0
self==2020.12.3
six==1.15.0
soupsieve==2.2
toml==0.10.2
urllib3==1.26.3
virtualenv==20.4.2
virtualenv-clone==0.5.4
wrapt==1.12.1

My azure-pipelines.yml:

# Python Function App to Linux on Azure
# Build a Python function app and deploy it to Azure as a Linux function app.
# Add steps that analyze code, save build artifacts, deploy, and more:
# https://docs.microsoft.com/azure/devops/pipelines/languages/python

trigger:
- master

variables:
  # Azure Resource Manager connection created during pipeline creation
  azureSubscription: '<subscription-Id>'

  # Function app name
  functionAppName: '<functionAppName>'

  # Agent VM image name
  vmImageName: 'ubuntu-latest'

  # Working Directory
  workingDirectory: '$(System.DefaultWorkingDirectory)/'

stages:
- stage: Build
  displayName: Build stage

  jobs:
  - job: Build
    displayName: Build
    pool:
      vmImage: $(vmImageName)

    steps:
    - bash: |
        if [ -f extensions.csproj ]
        then
            dotnet build extensions.csproj --runtime ubuntu.16.04-x64 --output ./bin
        fi
      workingDirectory: $(workingDirectory)
      displayName: 'Build extensions'

    - task: UsePythonVersion@0
      displayName: 'Use Python 3.7'
      inputs:
        versionSpec: 3.7 # Functions V2 supports Python 3.9 as of today

    - bash: |
        pip3 install -r requirements.txt

      workingDirectory: $(workingDirectory)
      displayName: 'Install application dependencies'

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

    - publish: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
      artifact: drop

- stage: Deploy
  displayName: Deploy stage
  dependsOn: Build
  condition: succeeded()

  jobs:
  - deployment: Deploy
    displayName: Deploy
    environment: 'development'
    pool:
      vmImage: $(vmImageName)

    strategy:
      runOnce:
        deploy:

          steps:
          - task: AzureFunctionApp@1
            displayName: 'Azure functions app deploy'
            inputs:
              azureSubscription: '$(azureSubscription)'
              appType: functionAppLinux
              appName: $(functionAppName)
              package: '$(Pipeline.Workspace)/drop/$(Build.BuildId).zip'

(注意:当然,在真实文件中包括我的订阅Id和FunctionAppName)

在构建时,我没有从Azure管道中得到任何错误。所有依赖项都已打包安装。我甚至亲自下载了.zip文件。老实说,在我看来,Azure函数似乎忽略了我的venv。有人能帮忙吗


Tags: 函数inpyimportbuildlinefunctionfunctions

热门问题