pythonlinuxazure-functionsconsumption

Function App deployment succeeds but fails to import packages (python, linux, consumption plan)


Local deployment directly of a function app using vscode succeeds and the function app works, however when trying to build and deploy through Azure Devops, the deployment succeeds but there is an error when importing numpy.

Result: Failure Exception: ImportError: Unable to import required dependencies: numpy: IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE! Importing the numpy C-extensions failed. This error can happen for many reasons, often due to issues with your setup or how NumPy was installed. We have compiled some common reasons and troubleshooting tips at: https://numpy.org/devdocs/user/troubleshooting-importerror.html Please note and check the following: * The Python version is: Python3.10 from "/usr/local/bin/python" * The NumPy version is: "1.25.0" and make sure that they are the versions you expect. Please carefully study the documentation linked above for further help. Original error was: No module named 'numpy.core._multiarray_umath' . Please check the requirements.txt file for the missing module. For more info, please refer the troubleshooting guide: https://aka.ms/functions-modulenotfound

azure-pipelines.yml is very similar to examples, install dependencies and zip:

variables:
  # Azure Resource Manager connection created during pipeline creation
  azureSubscription: 'azureSubscription'

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

  # 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
      inputs:
        versionSpec: '3.x' # string. Required. Version spec. Default: 3.x.
        architecture: 'x64' # 'x86' | 'x64'. Required. Architecture. Default: x64.

    - bash: |
        pip install --target="./.python_packages/lib/site-packages" -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

    - task: AzureFunctionApp@2
      inputs:
        azureSubscription: 'azureSubscription'
        appType: 'functionAppLinux'
        appName: 'app-name'
        package: '$(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip'
        deploymentMethod: 'auto'

The code is then deployed to the function:

steps:
- task: AzureFunctionApp@2
  displayName: 'Azure Function App Deploy'
  inputs:
    azureSubscription: 'azureSubscription'
    appType: functionAppLinux
    appName: 'app-name'
    package: '$(System.DefaultWorkingDirectory)/_Function App/drop/*zip'
    deploymentMethod: zipDeploy

Any help much appreciated, have spent hours looking at examples and reading through the documentation but still haven't got this working. Cheers.


Solution

  • In order to resolve your error make sure you have

    The NumPy version is: "1.25.0
    

    specified in your requirements.txt file in your Azure Function repository. And In your YAML Script set the Python version in UsePythonVersion@0 to 3.10 like my script below:-

    My requirements.txt:-

    azure-functions
    numpy==1.25.0
    

    enter image description here

    My YAML pipeline script:-

    trigger:
    - master
    
    variables:
      
      azureSubscription: 'subscription-id'
    
      
      functionAppName: 'siliconfunc56'
    
      
      vmImageName: 'ubuntu-latest'
    
      
      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.10'
          inputs:
            versionSpec: 3.10 
    
        - bash: |
            pip install --target="./.python_packages/lib/site-packages" -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'
    

    Output:-

    enter image description here

    Portal:-

    enter image description here

    enter image description here

    enter image description here