question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Exception: ModuleNotFoundError: No module named 'requests'

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:25 (3 by maintainers)

github_iconTop GitHub Comments

11reactions
antopolskiycommented, May 26, 2020

Had the same issue, can confirm that changing pip install -r requirements.txt line in my YAML file to pip install --target="$(workingDirectory)/.python_packages/lib/site-packages" -r requirements.txt resolved it.

11reactions
tomasslivkacommented, May 10, 2020

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

python - ImportError: No module named requests
Requests is not a built in module (does not come with the default python installation), so you will have to install it: OSX/Linux....
Read more >
[Fixed] ModuleNotFoundError: No module named 'requests'
How to Fix “ModuleNotFoundError: No module named 'requests'” in PyCharm · Open File > Settings > Project from the PyCharm menu. · Select...
Read more >
ModuleNotFoundError: No module named 'requests' in Python 3
If you are getting "ModuleNotFoundError: No module named 'requests'" error then it means either requests module is not installed or if it is ......
Read more >
ModuleNotFoundError: No module named 'requests' in Python
The Python "ModuleNotFoundError: No module named 'requests'" occurs when we forget to install the requests module before importing it or install it in...
Read more >
How to pip install the requests module to solve import errors?
No module named requests – even after i pip install ... Issue: you are still getting the import error even after executing the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found