Python azure http function issue while invocation

Sharma, Gaurav 0 Reputation points
2024-02-12T14:26:45.7366667+00:00

I have created an azure function app using python using http trigger. After deploying the app when I'm triggering the function,I'm getting following error in application insights. FUNCTIONS_EXTENSION_VERSION is ~4 Function app version is V1.If I use V2 then deployemnt is getting failed. following are the dependencies I'm using.

import logging
import azure.functions as func
from azure.storage.blob import BlobServiceClient,BlobClient, ContainerClient
import os
import docx
import PyPDF2
import re

and this is how my requirements.txt looks like (last code snippet)

azure-functions
azure-storage-blob
docx
Exception while executing function: Functions.<functionname>Result: Failure
Exception: ModuleNotFoundError: No module named 'exceptions'. 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 389, 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 194, 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 "/home/site/wwwroot/ChunkHttpTrigger/__init__.py", line 5, in <module>
    import docx
  File "/home/site/wwwroot/.python_packages/lib/site-packages/docx.py", line 30, in <module>
    from exceptions import PendingDeprecationWarning
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,908 questions
Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
8,930 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Ryan Hill 30,281 Reputation points Microsoft Employee Moderator
    2024-02-14T23:04:52.81+00:00

    Hi @Sharma, Gaurav
    There's something wrong with the docx package. My local setup is Python 3.11 with the same requirements.txt as you provided using the v2 programming model and the function wouldn't run locally. I received the same error message as you. When I removed the code reference to docx, the function built successfully and properly registered.

    I'll preface by saying I'm not well versed in docx, but in search I came across https://python-docx.readthedocs.io/en/latest/ and checking the python package library, saw https://pypi.org/project/python-docx/#history. So I attempted to use a lower version 1.01. which did not registered and received the following error message:

    ERROR: Could not find a version that satisfies the requirement docx==1.0.1 (from versions: 0.1.2, 0.2.0, 0.2.1, 0.2.2, 0.2.3, 0.2.4)
    ERROR: No matching distribution found for docx==1.0.1
    

    So, I then changed the requirements.txt to reference python-docx instead of docx and followed the docs using from docx import Document. Doing this resulted in a successful build and proper registration of the Azure Function locally. The code I used is very basic (as you can see below) but didn't result in any errors that docx resulted in. I don't know what the differences are, if any, between both, but I would advise using python-docx and see if that works for you. I also commented out PyPDF2 as that also resulted in a build/run failure. Could be a similar issue with the package just like python-docx.

    import logging
    import azure.functions as func
    from azure.storage.blob import BlobServiceClient,BlobClient, ContainerClient
    import os
    from docx import Document
    # import PyPDF2
    import re
    
    app = func.FunctionApp()
    
    @app.route(route="GetMessage", auth_level=func.AuthLevel.ANONYMOUS)
    def GetMessage(req: func.HttpRequest) -> func.HttpResponse:
        logging.info('Python HTTP trigger function processed a request.')
    
        try:
            filename = req.params.get('name')
            if not filename:
                return func.HttpResponse("Pass 'name' as a query string parameter.", status_code=400)
            
            doc = Document()
            doc.add_paragraph(req.get_body().decode('utf-8'))
            doc.save(f"{filename}.docx")
        except Exception as e:
            logging.error(str(e))
            return func.HttpResponse("An error occurred while processing the request.", status_code=500)
        
        return func.HttpResponse(f"File {filename}.docx created successfully.", status_code=200)
    
    0 comments No comments

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.