Python Azure Function Doesn't find modules even though installed in previous steps

36 Views Asked by At

I am trying to create a python azure function CI pipeline build. In the yml file, after installing the dependencies (pytest exists in requirements.txt), it cannot find pytest module. I cannot seem to figure what the issue is. What am I missing?

  - task: UsePythonVersion@0
    displayName: Use Python 3.11
    inputs:
      versionSpec: 3.11
  - task: Bash@3
    displayName: Install Application Dependencies
    inputs:
      targetType: inline
      script: >-
        python -m pip install --upgrade pip

        pip install --target="$(System.DefaultWorkingDirectory)/.python_packages/lib/site-packages" -r requirements.txt
  - script:  pytest . --verbose

Azure DevOps fails on pytest step where it says pytest can't be found.

requirements.txt

pytest~=8.1.1

What I've tried:

  • Adding --target (as you can see)
  • Adding target: host to pytest script
  • venv (which worked but I do not want to use venv)
1

There are 1 best solutions below

0
SiddheshDesai On

I tried adding pytest module in my requirements.txt and ran the Azure Devops yaml pipeline below, The Function and the package got deployed successfully.

My Azure Devops pipeline:-

trigger:
- main

variables:
  
  azureSubscription: '3xxxxxxxxxxx1a81b0e'

  
  functionAppName: 'valleyfunc765'

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

My requirements.txt:-

azure-functions
pytest

Output:-

enter image description here