Azure Function - Run and Debug locally - ModuleNotFoundError: No module named 'requests'

Lexignot 90 Reputation points
2024-05-26T03:51:00.5366667+00:00

Hello,

When I Run and Debug this simple HTTP Trigger function which includes the library Requests, I obtain the errors "ERROR: Error: No module named 'requests', Cannot find module." "ModuleNotFoundError: No module named 'requests", knowing the requests package is installed in my local environment. Azure Function Core Tools is also installed.

Do you have any thoughts where this error could be coming from?

Thanks in advance for your help with this!

Versions:

  • Python 3.11.9
  • Azure Function Core Tools 4.0.5801
Package                Version
---------------------- --------
azure-core             1.30.1  
azure-functions        1.19.0  
azure-identity         1.16.0  
azure-keyvault-secrets 4.8.0   
certifi                2024.2.2
cffi                   1.16.0  
charset-normalizer     3.3.2   
cryptography           42.0.7  
idna                   3.7     
isodate                0.6.1   
msal                   1.28.0  
msal-extensions        1.1.0   
packaging              24.0    
pip                    24.0    
portalocker            2.8.2   
pycparser              2.22    
PyJWT                  2.8.0   
pywin32                306     
requests               2.32.2
setuptools             65.5.0
six                    1.16.0
typing_extensions      4.12.0
urllib3                2.2.1

requirements.txt

# DO NOT include azure-functions-worker in this file
# The Python Worker is managed by Azure Functions platform
# Manually managing azure-functions-worker may cause unexpected issues

azure-functions
requests
azure-identity
azure-keyvault-secrets
msal

local.settings.json

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "python",
    "PYTHON_PATH": ".venv\\Scripts\\python.exe",
    "AzureWebJobsFeatureFlags": "EnableWorkerIndexing"
  }
}

host.json

{
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    }
  },
  "extensionBundle": {
    "id": "Microsoft.Azure.Functions.ExtensionBundle",
    "version": "[4.*, 5.0.0)"
  }
}

function_app.py

import logging
import requests
import azure.functions as func

def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')
    response = requests.get("https://httpbin.org/get")
    return func.HttpResponse(f"Response: {response.status_code}")

.vscode - extensions.json

{
  "recommendations": [
    "ms-azuretools.vscode-azurefunctions",
    "ms-python.python"
  ]
}

.vscode - launch.json


{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Attach to Python Functions",
      "type": "debugpy",
      "request": "attach",
      "connect": {
        "host": "localhost",
        "port": 9091
      },
      "preLaunchTask": "func: host start"
    }
  ]
}

.vscode - settings.json

{
  "azureFunctions.deploySubpath": ".",
  "azureFunctions.scmDoBuildDuringDeployment": true,
  "azureFunctions.pythonVenv": ".venv",
  "azureFunctions.projectLanguage": "Python",
  "azureFunctions.projectRuntime": "~4",
  "debug.internalConsoleOptions": "neverOpen",
  "azureFunctions.projectLanguageModel": 2,
  "python.defaultInterpreterPath": "C:\\<path>\\.venv\\Scripts\\python.exe" #Real path here
}

.vscode - tasks.json


{
  "version": "2.0.0",
  "tasks": [
    {
      "type": "func",
      "label": "func: host start",
      "command": "host start",
      "options": {
        "env": {
          "PYDEVD_DISABLE_FILE_VALIDATION": "1" 
        }
      },
      "problemMatcher": "$func-python-watch",
      "isBackground": true,
      "dependsOn": "pip install (functions)"
    },
    {
      "label": "pip install (functions)",
      "type": "shell",
      "osx": {
        "command": "${config:azureFunctions.pythonVenv}/bin/python -m pip install -r requirements.txt"
      },
      "windows": {
        "command": "${config:azureFunctions.pythonVenv}\\Scripts\\python -m pip install -r requirements.txt"
      },
      "linux": {
        "command": "${config:azureFunctions.pythonVenv}/bin/python -m pip install -r requirements.txt"
      },
      "problemMatcher": []
    }
  ]
}
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,944 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Lexignot 90 Reputation points
    2024-05-27T23:56:08.4733333+00:00

    Hello @Gowtham CP

    Thanks for your answer. I solved the issue with the following:

    1. Re-install Python 3.11.9 from the official Python website. Previously I installed it from the Microsoft store and apparently this is a known issue with Azure Function. See here: link
    2. I had to add the following decorators to my script in the function_app.py just before calling the main function:
         @app.function_name(name="Http_Trigger1")
         @app.route(route="Http_Trigger1")
      
    1 person found this answer helpful.
    0 comments No comments

  2. JananiRamesh-MSFT 26,631 Reputation points
    2024-05-28T15:25:21.9033333+00:00

    @Lexignot glad to see you were able to resolve your issue. Thanks for posting your solution so that others experiencing the same thing can easily reference this.

    Since the Microsoft Q&A community has a policy that  the question author cannot accept their own answer, they can only accept answers by others, I'll repost your solution in case you'd like to Accept the answer.

    Issue: When you Run and Debug simple HTTP Trigger function which includes the library Requests, you received the following error "ERROR: Error: No module named 'requests', Cannot find module." "ModuleNotFoundError: No module named 'requests".

    Resolution:

    You solved the issue with the following:

    1. Re-install Python 3.11.9 from the official Python website. Previously I installed it from the Microsoft store and apparently this is a known issue with Azure Function. See here: link
    2. I had to add the following decorators to my script in the function_app.py just before calling the main function:
         @app.function_name(name="Http_Trigger1")
         @app.route(route="Http_Trigger1")
      
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.