question

29705083 avatar image
1 Vote"
29705083 asked ChaitanyaNaykodiMSFT-9638 answered

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

I have been following this tutorial [https://docs.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-functionsazure-form-recognizer
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

ChaitanyaNaykodiMSFT-9638 avatar image
1 Vote"
ChaitanyaNaykodiMSFT-9638 answered

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.


· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi!
I'm using python 3.8.10.

I've tried installing from requirements.txt without any success. However, I now tried to install the packages outside of the virtual enviroment, and it works...

It would however be nice to contain all packages in a venv,

0 Votes 0 ·

Hello @Asselman, thank you for the response. I think this issue is related to virtual environment set-up for Python in VS code. Could you please create an issue on the vscode-azurefunctions repo with appropriate reproducing steps?
Please let me know if there are any concerns. Thank you!



1 Vote 1 ·