Home > other >  ModuleNotFoundError: No module named 'azure.cosmos' on azure function in visual studio cod
ModuleNotFoundError: No module named 'azure.cosmos' on azure function in visual studio cod

Time:05-09

I've been trying to incorporate azure.cosmos into a azure function without using binding since the scenario isn't beneficial for it.

The problem every time i try to import it i get ModuleNotFoundError as follow:

[2022-05-04T03:00:41.809Z] Worker failed to load function: 'HttpTrigger1' with function id: '8b8c7e22-7d68-4531-93b0-f43dcc2e7f6c'. [2022-05-04T03:00:41.809Z] Result: Failure Exception: ModuleNotFoundError: No module named 'azure.cosmos'. Troubleshooting Guide: https://aka.ms/functions-modulenotfound Stack: File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.9/WINDOWS/X64\azure_functions_worker\dispatcher.py", line 305, in _handle__function_load_request func = loader.load_function( File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.9/WINDOWS/X64\azure_functions_worker\utils\wrappers.py", line 42, in call raise extend_exception_message(e, message) File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.9/WINDOWS/X64\azure_functions_worker\utils\wrappers.py", line 40, in call return func(*args, **kwargs) File "C:\Program Files\Microsoft\Azure Functions Core Tools\workers\python\3.9/WINDOWS/X64\azure_functions_worker\loader.py", line 85, in load_function mod = importlib.import_module(fullmodname) File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3312.0_x64__qbz5n2kfra8p0\lib\importlib\__init__.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "C:\Users\micha\Desktop\azure durable test\HttpTrigger1\__init__.py", line 4, in <module> import azure.cosmos

I've double check the requirements.txt and it is as follows:

azure-functions azure-cosmos == 4.3.0b4

I've also double check the .env file for the module and its there

image

I'm using Azure Function Core tools 4.0.4483 along with Python 3.9 from Microsoft Store.

Lastly, i can in fact run the script with the venv python interpreter without issues but like mentioned unable to do so when debugging from local function simulation via F5 command which throws the mentioned error.

I'm also having the same issue with azure durable functions module so I'm really are at loss here since the current workaround isn't the most optimal for my use case.

Edit:

this is code which its just the sample code you get from creating an azure function in visual studio code

import logging

import azure.functions as func
import azure.cosmos

def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    name = req.params.get('name')
    if not name:
        try:
            req_body = req.get_json()
        except ValueError:
            pass
        else:
            name = req_body.get('name')

    if name:
        return func.HttpResponse(f"Hello, {name}. This HTTP triggered function executed successfully.")
    else:
        return func.HttpResponse(
             "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
             status_code=200
        )


CodePudding user response:

Noticed in your above stacktrace i see this:
"C:\Users\micha\Desktop\azure durable test\HttpTrigger1_init_.py", line 4, in import azure.cosmoss

Two s's in the azure.cosmoss in your code, you said it works locally? Can you double check the file in kudu assuming this isn't consumption on windows or check the file via ftp or before you deploy to ensure you are importing azure.cosmos, not cosmoss!

Hope that is it.

CodePudding user response:

I had the same error and for me it was related to that "running scripts" was not allowed as was set in the Execution Policy of the local machine. Because of this the .venv environment (with azure.cosmos included) didn't get activated. I ran the "Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser" command in a Powershell terminal from Visual Studio Code and that helped resolving it for me.

  • Related