Sutapa
Sutapa

Reputation: 21

Azure Function in Python not able to use pymongo

I have a MongoDB running in a AKS and a load balancer created for exposing it outside AKS cluster. I also have set the bindIP in mongod.conf using below command

sed "s,\\(^[[:blank:]]*bindIp:\\) .*,\\1 0.0.0.0," /etc/mongod.conf
mongod --config C:\mongodb\conf\mongodb.conf

I have an Azure function written in python to connect to the db.

I am new to python and just trying out the basic code to connect to the mongodb.

import logging

import azure.functions as func
import pymongo


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:
    elif name:
        uri = "mongodb://User:[email protected]:27017/testdb"
        client = pymongo.MongoClient(uri)

        db = client.test
        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
        )

When I am trying to run the code I am getting the below error, any help is appreciated.

Result: Failure Exception: ModuleNotFoundError: No module named 'pymongo'. 
Troubleshooting Guide: https://aka.ms/functions-modulenotfound 
Stack: File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/dispatcher.py", 
line 315, in _handle__function_load_request func = loader.load_function( 
File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/utils/wrappers.py", 
line 42, in call raise extend_exception_message(e, message) File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/utils/wrappers.py", 
line 40, in call return func(*args, **kwargs) File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/loader.py", 
line 85, in load_function mod = importlib.import_module(fullmodname) File "/usr/local/lib/python3.9/importlib/__init__.py", 
line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "/home/site/wwwroot/HttpTrigger1/__init__.py", 
line 4, in <module> import pymongo

Upvotes: 2

Views: 402

Answers (1)

Anass Kartit
Anass Kartit

Reputation: 2088

There are several reasons the error might be related to as described here:

  1. How do you deploy (VSCode, Azure Devops) is the requirements.txt part of the code deployed?
  2. is the pymongo compatible with python version of your azure function? it is compatible according to this https://pypi.org/project/pymongo/3.9.0/.
  3. the function has WEBSITE_RUN_FROM_PACKAGE set to 1?

you need to troubleshoot and check the guide in here: https://aka.ms/functions-modulenotfound

Upvotes: 1

Related Questions