Hisham Alfoqaha
Hisham Alfoqaha

Reputation: 258

Import/ install Pandas and Matplotlib on Azure Function

I'm trying to import python libraries to Azure Function, But when I run the code locally and on the server, I get a message that these libraries are not available! "No module named 'pandas' ", No module named 'pandas'., ...etc I added them to the requirements.txt file and I'm still getting the same error

The Output

Executed 'Functions.PandasTest' (Failed, Id=298034b9-f97d-4610-850a-c5fd67d5bccb, Duration=3ms) [2022-07-21T10:34:59.0052] System.Private.Corelib: Exception while executing function: Functions.PandasTest. System.Private.Corelib: Result: Failure Exception: ModuleNotFoundError: No module named 'pandas'. 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 315, in _handle_function_load_request

requirements.txt

azure-functions
nltk
numpy
pandas
six

init.py

import logging
import azure.functions as func
import pandas


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

    import pandas

    mydataset = {'cars': ["BMW", "Volvo", "Ford"], 'passings': [3, 7, 2] }

    myvar = pandas.DataFrame(mydataset)

    #Retreive Method type, it tells us what kind of request we're receiving
    method_type = req.method

    if method_type == "GET":
        name = req.params.get('name') #getting the name argument from a get request from req "func.HttpRequest" :- we check if this request contains a 'name' argument in it
    
    elif method_type == "POST":
        try:
            req_body = req.get_json()  #check if the request contains json "if we recieve a json", if this code execute without any errors, we go to the else statement, if there is any exceptions in this line we go to except ValueError:
        except ValueError:
            name = None
        else:
            name = req_body.get('name')  #get the name value

    if name:  #we check if the name was found
        return func.HttpResponse(f"Hello, {name}. This HTTP triggered function executed successfully.", status_code=200)
    else:
        return func.HttpResponse(
             "Pass a name in the query string (GET request) or a JSON body (POST request)",
             status_code=400
        )

Upvotes: 0

Views: 777

Answers (1)

AjayKumarGhose
AjayKumarGhose

Reputation: 4943

One of the workaround you can follow to resolve the above issue,

After seeing your error logs, it seems that you have not configured properly pandas in your environment (i.e in local).

Once you have created your Azure function and trying to use pandas for your requirement . As suggested by @ DeepDave-MT in comment this SO THREAD try to run the command ;

pip freeze > requirements.txt

It will show the requirement.txt file of your project and then add pandas and all your reuqired SDKs which will need to use for your environment .

Then save and run the cmd:- pip install -r requirements.txt then test your function it should work.

REFERENCE SNAPSHOT:- enter image description here enter image description here

enter image description here For more information please this MICROSOFT DOCUMENTATION| Install required Python Azure SDK libraries

Upvotes: 0

Related Questions