Azure Functions Python install local packages

Jorge Lopez 41 Reputation points
2024-11-11T13:39:30.6066667+00:00

Hi everyone,

I'm facing issues deploying an Azure Function that relies on a custom local package within a monorepo setup. We have an internal package shared across multiple apps in our repo, and the Azure Function imports this module for specific computations.

In requirements.txt I added an editable install pointing to our shared package:

azure-functions
-e ../libs/new_package

In function_app.py, the setup works fine locally with the following code:

import azure.functions as func
import logging
from new_package.example import hello_from_module


app = func.FunctionApp(http_auth_level=func.AuthLevel.ANONYMOUS)

@app.route(route="test_function")
def test_function(req: func.HttpRequest) -> func.HttpResponse:
    logging.info(hello_from_module())
    return func.HttpResponse(f"Module imported: {hello_from_module()}. This HTTP triggered function executed successfully.")

When running locally with the debugger, the function performs as expected. However, when I attempt to deploy to Azure using: func azure functionapp publish my_function_app --build local (as the official documentation suggest) the function doesn’t appear in Azure, and I’m not receiving any errors or deployment logs to indicate what might be wrong. I also tried deploying through Azure DevOps pipelines but encountered the same issue.

variables:    # Azure Resource Manager connection created during pipeline creation
  - name: azureSubscription
    value: ***

  - name: functionAppName
    value: my_function_app

  - name: vmImageName
    value: ubuntu-latest

  - name: workingDirectory
    value: $(System.DefaultWorkingDirectory)/python_monorepo/my_function_app

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.9'
      inputs:
        versionSpec: 3.9 # Functions V2 supports Python 3.6 as of today

    - 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@2
            displayName: 'Azure functions app deploy'
            inputs:
              azureSubscription: '$(azureSubscription)'
              appType: functionAppLinux
              appName: $(functionAppName)
              package: '$(Pipeline.Workspace)/drop/$(Build.BuildId).zip'

Oddly enough, if I remove the local package dependency, the deployment works fine.

Has anyone successfully deployed an Azure Function with a similar setup, or are there specific steps to ensure Azure recognizes local modules in monorepos? Any advice or insights would be greatly appreciated!

Thank you! Edit: for additional context I'm using the Linux Consumption Plan

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,192 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.