milesb
milesb

Reputation: 99

Python module not found Azure function app

I'm trying to deploy a Python application to an Azure Function App via Azure DevOps.

In my build pipeline I can see that it successfully installs the required package Pygresql from my requirements.txt in the root folder of the project, but then I get a module not found message when I deploy to the app in Azure.

Build pipeline success message Please find my build pipeline configuration below:

pool:
name: Azure Pipelines

trigger:
branches:
    include:
    - master
    
steps:
- bash: |
  if [ -f extensions.csproj ]
  then
      dotnet build extensions.csproj --output ./bin
  fi
 displayName: 'Build extensions'

 - task: UsePythonVersion@0
   displayName: 'Use Python 3.7'
 inputs:
   versionSpec: 3.7

 - script: |
    python -m venv worker_venv
    source worker_venv/bin/activate
    python -m pip install --upgrade pip
    pip install setuptools
    pip install setup
    pip install -r requirements.txt
 displayName: 'Install Application Dependencies'

 - task: ArchiveFiles@2
 displayName: 'Archive files'
 inputs:
   rootFolderOrFile: '$(System.DefaultWorkingDirectory)'
   includeRootFolder: false

- task: PublishBuildArtifacts@1
  displayName: 'Publish Artifact: drop'

Any help would be greatly appreciated!

Many thanks,

Miles

Upvotes: 1

Views: 2053

Answers (1)

Delliganesh Sevanesan
Delliganesh Sevanesan

Reputation: 4786

The ways to fix the module not found error:

Way 1

Specify your python version in your Script portion

.
.
.
# either you can use script / bash. Here i am using bash
 - bash: |
    python3.7 -m venv worker_venv
    source worker_venv/bin/activate
    python3.7 -m pip install --upgrade pip
    pip3.7 install setuptools
    pip3.7 install setup
    pip3.7 install -r requirements.txt
 displayName: 'Install Application Dependencies'
.
.
.

Refer build CI/CD for python azure functions using Azure DevOps

Way 2

Instead of using pip install -r requirements.txt try using below

# for python version 3.7
pip install --target="./.python_packages/lib/site-packages" -r ./requirements.txt

# for python version 3.6
pip install --target="./.python_packages/lib/python3.6/site-packages" -r ./requirements.txt

Refer example yaml build pipeline using python azure function.

Way 3

Instead of using python -m venv worker_venv try using python -m venv .python_packages

Refer the github issue for information.

Upvotes: 2

Related Questions