Home > front end >  Unable to import AppendBlobService from azure.storage.blob in Azure function
Unable to import AppendBlobService from azure.storage.blob in Azure function

Time:06-30

I am using the below azure function which takes http trigger as input.

import logging
import azure.functions as func
from . import test

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

    name = req.params.get('name')
    strng = req.params.get('strng')
    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.sum = {test.testfunc(strng)}")
    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
        )

Below is the test.py file which I imported in the init.py.

import json
import pandas as pd
from pandas import DataFrame
from azure.storage.blob import AppendBlobService
from datetime import datetime


def testfunc(strng):
    # return strng
    API = json.loads(strng)

    test = pd.json_normalize(parse, record_path='Vol', meta=['studyDate'])
    df = pd.DataFrame(test)
    df["x"] = df["Vol"] * 2
    df["y"] = df["Vol"] * 50
    df1 = df[['Date', 'x', 'y']]
    df2 = df1.to_json(orient='records')
    append_blob_service = AppendBlobService(account_name='actname',
                                         account_key='key')
    date = datetime.now()
    blobname = f"test_cal{date}.json"
    append_blob_service.create_blob('container', blobname, if_none_match="*")
    append_blob_service.append_blob_from_text('container', blobname, text=df2)
    return df2
    

The above function works when I run it in pycharm and databricks . But when I run the Azure function via visuals studio code, I get the below error.

Exception: ImportError: cannot import name 'AppendBlobService' from 'azure.storage.blob'

I have tried below and still get the same error.

pip install azure-storage --upgrade
pip install azure-storage-blob

Kindly provide assistance with the error.

Is there any other way I can save the df2 variable to Azure storage.

Thank you.

CodePudding user response:

According to Document it says,

If the module (for example, azure-storage-blob) cannot be found, Python throws the ModuleNotFoundError. If it is found, there may be an issue with loading the module or some of its files. Python would throw a ImportError in those cases.

  • Try setting python to environment variable FUNCTIONS_WORKER_RUNTIME or
  • Try adding azure.core to your requirements.txt file.

References:

ImportError: cannot import name 'BlockBlobService' from 'azure.storage.blob'

CodePudding user response:

The current version library contains the blobserviceclient instead of the blockblockblockservice. This works for me by using version 2.1.0 can solve it:

pip install azure-storage-blob==2.1.0
  • Related