Azure Functions Locally ModuleNotFoundError: No module named 'azure.storage.blob'

29705083 6 Reputation points
2021-07-20T13:21:54.727+00:00

I have been following this tutorial [https://learn.microsoft.com/en-us/azure/cognitive-services/form-recognizer/tutorial-azure-function?tabs=2-0][1]

Im writing in VS Code and python, on a windows computer.

I'm trying to create an Azure function with Blob Trigger. When I test the function locally, both in VS code and cmd I get the following error:

Exception: ModuleNotFoundError: No module named 'azure.storage.blob'. Troubleshooting Guide: https://aka.ms/functions-modulenotfound
from azure.storage.blob import BlobServiceClient

I've added the modules in requirements.txt:

azure-functions  
cryptography  
azure-storage-blob  
azure-identity  
requests  
pandas  
numpy  

function.json looks like this

{  
  "scriptFile": "__init__.py",  
  "bindings": [  
    {  
      "name": "myblob",  
      "type": "blobTrigger",  
      "direction": "in",  
      "path": "container/{name}",  
      "connection": "<storageaccount>"  
    }  
  ]  
}  

Local settings:

{  
  "IsEncrypted": false,  
  "Values": {  
    "AzureWebJobsStorage": "<connectionstring>",  
    "FUNCTIONS_WORKER_RUNTIME": "python",  
    "tetrastorageaccount_STORAGE": "<connectionstring>"  
  }  
}  

And init looks like this:

import logging  
import azure.functions as func  
from azure.storage.blob import BlobServiceClient  
import json  
import time  
import requests  
import os  
from collections import OrderedDict  
import numpy as np  
import pandas as pd  
  
def main(myblob: func.InputStream):  
    logging.info(f"Python blob trigger function processed blob \n"  
                 f"Name: {myblob.name}\n"  
                 f"Blob Size: {myblob.length} bytes")  
  
    # This is the call to the Form Recognizer endpoint  
    endpoint = r"<endpoint>"  
    apim_key = "<key>"  
    post_url = endpoint + "/formrecognizer/v2.0/Layout/analyze"  
    source = myblob.read()  
  
    headers = {  
    # Request headers  
    'Content-Type': 'application/pdf',  
    'Ocp-Apim-Subscription-Key': apim_key,  
        }  
  
    text1=os.path.basename(myblob.name)  
  
    resp = requests.post(url = post_url, data = source, headers = headers)  
    if resp.status_code != 202:  
        print("POST analyze failed:\n%s" % resp.text)  
        quit()  
    print("POST analyze succeeded:\n%s" % resp.headers)  
    get_url = resp.headers["operation-location"]  
  
    wait_sec = 25  
      
    time.sleep(wait_sec)  
    # The layout API is async therefore the wait statement  
      
    resp =requests.get(url = get_url, headers = {"Ocp-Apim-Subscription-Key": apim_key})  
      
    resp_json = json.loads(resp.text)  
      
      
    status = resp_json["status"]  
      
      
    if status == "succeeded":  
        print("Layout Analysis succeeded:\n%s")  
        results=resp_json  
    else:  
        print("GET Layout results failed:\n%s")  
        quit()  
  
    results=resp_json  
  
    # This is the connection to the blob storage, with the Azure Python SDK  
    blob_service_client = BlobServiceClient.from_connection_string("<connection_string>")  
    container_client=blob_service_client.get_container_client("container")  
  
    # Here is the upload to the blob storage  
    container_client.upload_blob(name="test",data=results)  
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,131 questions
Azure AI Document Intelligence
Azure AI Document Intelligence
An Azure service that turns documents into usable data. Previously known as Azure Form Recognizer.
1,724 questions
0 comments No comments
{count} vote

1 answer

Sort by: Most helpful
  1. ChaitanyaNaykodi-MSFT 26,366 Reputation points Microsoft Employee
    2021-07-20T23:52:43.697+00:00

    Hello @46268530,
    What version of Python you are using currently?
    Can you please try and install the packages in requirements.txt by running the command pip install -r requirements.txt? and then execute the code.

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.