azure-functions-python-worker: Exception: ModuleNotFoundError: No module named 'requests'
I’m trying to run a simple python script via an Azure Function. When I run the function locally, it works fine. But when I deploy the function to Azure using Azure Pipelines, I encounter the ModuleNotFoundError for requests even though I’ve included the request in requirements.txt. I saw some other people ran into this issue such as 626, I tried the solutions in these posts but haven’t got anything to work.
I suspect that the packages installed in .python_packages/lib/site-packages are not being read in the Azure Portal or becuase I am using Linux & Python in “Consumption Plan”.
Investigative information
Please provide the following:
- Function App name: TakeRateFunction
Repro steps
Provide the steps required to reproduce the problem:
I use the following YAML in my Azure Pipelines Build:
# 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. Replace 'xxx' with actual subscription and functionappname
azureSubscription: 'xxxx'
# Function app name
functionAppName: 'xxxx'
# Agent VM image name
vmImageName: 'ubuntu-latest'
# Working Directory
workingDirectory: '$(System.DefaultWorkingDirectory)/my_repo'
stages:
- stage: Build
displayName: Build stage
jobs:
- job: Build
displayName: Build
pool:
vmImage: $(vmImageName)
steps:
- task: UsePythonVersion@0
displayName: 'Use Python 3.6'
inputs:
versionSpec: 3.6 # Functions V2 supports Python 3.6 as of today
- bash: |
if [ -f extensions.csproj ]
then
dotnet build extensions.csproj --output ./bin
fi
pip install --target $(System.DefaultWorkingDirectory)/my_repo/.python_packages/lib/site-packages -r requirements.txt
- 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: 'production'
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'
Expected behavior
Provide a description of the expected behavior.
Actual behavior
Azure Function is able to find requests locally but not on Portal.I get the following error in the portal:
Result: Failure
Exception: ModuleNotFoundError: No module named 'requests'
Stack: File "/azure-functions-host/workers/python/3.6/LINUX/X64/azure_functions_worker/dispatcher.py", line 242, in _handle__function_load_request
func_request.metadata.entry_point)
File "/azure-functions-host/workers/python/3.6/LINUX/X64/azure_functions_worker/loader.py", line 66, in load_function
mod = importlib.import_module(fullmodname)
File "/usr/local/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "/home/site/wwwroot/TakeRateFunction/update.py", line 13, in <module>
from ..modules.take_rate_wrapper.take_rate import TakeRate
File "/home/site/wwwroot/modules/take_rate_wrapper/take_rate.py", line 6, in <module>
import requests
Known workarounds
I have tried Azure CLI instead of Azure Pipelines YAML and still getting the same error.
Contents of the requirements.txt file:
Provide the requirements.txt file to help us find out module related issues.
Related information
Tried these links but none of them helped so far
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 29 (3 by maintainers)
Had the same issue, can confirm that changing
pip install -r requirements.txtline in my YAML file topip install --target="$(workingDirectory)/.python_packages/lib/site-packages" -r requirements.txtresolved it.I have lost few hours with this issue and I have found that this happens when you select Python 3.7/3.8 during the function creation in Azure and later you are trying to integrate the Azure Pipeline which supports Python 3.6 only. So this is not a bug, but it’s really difficult to realize the root cause if you have created your function a time ago. I guess that Python 3.6 Azure Pipeline has different directory structure than Azure Function for Python 3.7/3.8 expects so site packages are not found. Maybe some note in the docs or check for the version of the target function would help further investigators.
Thanks @cmasch. You were spot on. I missed this gem,
FUNCTIONS_WORKER_RUNTIMEin my terraformapp_settingsdefinition, but I had it in mylocal.settings.jsonand skimmed right past it in the terraform docs. For anyone else that may stumble upon this, this worked for me:And there was a very clear difference in the build output:
Hi Joey, do you use Terraform for deploying the function app? I guess so 😃 Could you pls try out to create a function app via the portal with Python as runtime? If you publish the app again, do you get some outputs about pip and installing dependencies?
If you use Terraform, could you provide the Terraform code and the provider version? Maybe the Python runtime is not specified in the app settings (see azurerm_function_app). That should fix the issue: https://docs.microsoft.com/en-us/azure/azure-functions/functions-app-settings#functions_worker_runtime
Best Christopher
Although i think the docs might be worded a bit differently: From what it says now it seems like including deps. in the .python_packages dir is only relevant when you want to include dependencies that are not publicly available (e.g not on pip), but all of my dependencies are on pip.
This is not a hack. If for some reason you are not able to use Remote Build then this is the right way to include your dependencies. https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-python#custom-dependencies
Sure, i made pip install to $(wd)/.python_packages. This makes the packages available to anything running from wd.
Nevermind, fixed this by making pip install to WD/.python_packages…
@stefanlenoach I published my functions with a remote build option with this command:
func azure functionapp publish my_package --build remoteIn my requirements.txt I had a package called
sqlalchemy-snowflake. Now, the pip version used by Azure’s Dockerfile is probably using an older version of pip. In my local, I am using pip 20.0.2 while the Azure’s remote build is using 19.2.3. During my build it even gives me the following error:You are using pip version 19.2.3, however version 20.0.2 is available.So I removed sqlalchemy-snowflake and it removed the error for me. So change your local pip version to 19.2.3 and see if you still get the same error.If yes, then you know it is pip version issue.
Sorry for some reason, it didn’t publish. Here is the requirements.txt