Exception while executing function app using Python

Venkata Naga Sukumar Vinnakota 0 Reputation points
2023-05-16T22:45:23.31+00:00

I am executing a code to fetch data from API and store in Storage account. I have written the code in Python and used data frame, requests and azure blob modules. I am getting the below exception when I try to execute my code.

Please help me in this regard.

Exception:

Exception while executing function: Functions.code Result: Failure
Exception: ImportError: Unable to import required dependencies:
numpy: 

IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!

Importing the numpy C-extensions failed. This error can happen for
many reasons, often due to issues with your setup or how NumPy was
installed.

We have compiled some common reasons and troubleshooting tips at:

    https://numpy.org/devdocs/user/troubleshooting-importerror.html

Please note and check the following:

  * The Python version is: Python3.9 from "/usr/local/bin/python"
  * The NumPy version is: "1.24.3"

and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.

Original error was: No module named 'numpy.core._multiarray_umath'
. Please check the requirements.txt file for the missing module. For more info, please refer the troubleshooting guide: https://aka.ms/functions-modulenotfound
Stack:   File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/dispatcher.py", line 370, 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 48, 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 44, in call
    return func(*args, **kwargs)
  File "/azure-functions-host/workers/python/3.9/LINUX/X64/azure_functions_worker/loader.py", line 132, 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 "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 850, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "/home/site/wwwroot/code/__init__.py", line 4, in <module>
    import pandas
  File "/home/site/wwwroot/.python_packages/lib/site-packages/pandas/__init__.py", line 16, in <module>
    raise ImportError(

Code:

import logging
import requests
import azure.functions as func
import pandas
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')
    url = "https://api.themoviedb.org/3/trending/movie/day?language=en-US"

    headers = {
        "accept": "application/json",
        "Authorization": "Bearer xxxxxxxxxxxx"
    }
    response = requests.get(url, headers=headers).json()
    response_list = response['results']
    df = pandas.DataFrame(response_list)
    df_selected = df[['title','overview','release_date','vote_count']]
    print(df_selected)
    output= df_selected.to_csv(index = None)
    blob_service_client = BlobServiceClient.from_connection_string("xxxxxxxxxxxx")
    blob = BlobClient.from_connection_string("DefaultEndpointsProtocol=https;AccountName=;AccountKey=;EndpointSuffix=core.windows.net", container_name='', blob_name='output.csv')
    with open('output.csv', "w") as data:
        blob.upload_blob(output,overwrite=True)
    
    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
        )

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,258 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Ryan Hill 25,666 Reputation points Microsoft Employee
    2023-05-20T04:50:30.0766667+00:00

    @Venkata Naga Sukumar Vinnakota

    I was able to get this to work after some tweaks to your current code. Here's what I have that worked.

    __init__.py

    import logging
    import os
    
    import azure.functions as func
    import azure.storage.blob as blob
    import pandas as pd
    import requests
    
    def main(req: func.HttpRequest) -> func.HttpResponse:
        logging.info('Python HTTP trigger function processed a request.')
    
        # Get the data from the API
        url = "https://api.themoviedb.org/3/trending/movie/day?language=en-US"
        headers = {
            "accept": "application/json",
            "Authorization": f"Bearer {os.getenv('API_KEY')}"
        }
        response = requests.get(url, headers=headers)
        results = response.json()["results"]
    
        # Create a dataframe from the results
        df = pd.DataFrame(results)
        df_selected = df[["id", "title", "overview", "poster_path", "vote_average", "vote_count"]]
        output = df_selected.to_csv(index=False)
        logging.info(df_selected.head())
    
        # Upload the csv to blob storage
        # Create a blob client using the local file name as the name for the blob
        try:
            blob_service_client = blob.BlobServiceClient.from_connection_string(os.getenv('AZURE_STORAGE_CONNECTION_STRING'))
            container_name = "movies"
            blob_name = "trending.csv"
            container_client = blob_service_client.get_container_client(container_name)
            if(not container_client.exists()):
                container_client.create_container()
            blob_client = container_client.get_blob_client(blob=blob_name)
            blob_client.upload_blob(output, blob_type="BlockBlob")
        except Exception as ex:
            logging.error('Exception:')
            logging.error(ex)
            return func.HttpResponse("Error occurred processing request", status_code=500)
    
        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
        )
    

    I'm using the latest modules in my 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
    azure-storage-blob
    pandas
    requests
    
    

    Locally, I'm using 3.10 interpreter because for some reason, I couldn't get the imports to work correctly. Even though they were installed, when debugging the function app locally, it would also report a module load error. On the deployed function app, it's using python 3.8 runtime. I did also have an issue on the function app where for whatever reason, the function app didn't create container whereas locally, it did. I created the container manually and got the 200.

    The main difference is I'm aliasing some of the imported modules, so try that and see if that resolves your issue.