Exception: ModuleNotFoundError: No module named 'requests'
See original GitHub issueI’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
Issue Analytics
- State:
- Created 4 years ago
- Comments:25 (3 by maintainers)
Top GitHub Comments
Had the same issue, can confirm that changing
pip install -r requirements.txt
line in my YAML file topip install --target="$(workingDirectory)/.python_packages/lib/site-packages" -r requirements.txt
resolved 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.