pythonazureazure-functions

Running an Azure Function locally results in a worker error


I created a local Azure function project on Windows using the following CLI commands from the Azure documentation:

func init --python
 
func new --name HttpExample --template "HTTP trigger" --authlevel "anonymous"

Running these commands results in the following Azure function being created:

import azure.functions as func
import datetime
import json
import logging

app = func.FunctionApp()

@app.route(route="HttpExample", auth_level=func.AuthLevel.anonymous)
def HttpExample(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')

    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
        )

The requirements.txt created by the Azure Function Core Tools includes only the azure-functions library.

After starting the function with the func start command, the worker exits with the following error:

Found Python version 3.12.0 (py).

Azure Functions Core Tools
Core Tools Version:       4.0.6610 Commit hash: N/A +0d55b5d7efe83d85d2b5c6e0b0a9c1b213e96256 (64-bit)
Function Runtime Version: 4.1036.1.23224

[2024-12-20T13:20:17.095Z]  ERROR: unhandled error in functions worker: '_SixMetaPathImporter' object has no attribute '_path'
[2024-12-20T13:20:17.127Z] Language Worker Process exited. Pid=13732.
[2024-12-20T13:20:17.128Z] py exited with code 1 (0x1). AttributeError: '_SixMetaPathImporter' object has no attribute '_path',AttributeError: '_SixMetaPathImporter' object has no attribute '_path',AttributeError: '_SixMetaPathImporter' object has no attribute '_path'.
[2024-12-20T13:21:15.126Z] Starting worker process failed
[2024-12-20T13:21:15.129Z] Microsoft.Azure.WebJobs.Script.Grpc: The operation has timed out.
[2024-12-20T13:21:15.133Z] Failed to start language worker process for runtime: python. workerId:ed1a4b3d-07d0-41f7-9116-2f60397a6748

How can I fix this?

What I tried:


Solution

  • You are getting error because of the invalid authlevel provided in the func new command.

    You should change the anonymous to ANONYMOUS in the command.

    Change the command as below:

    func new --name HttpExample --template "HTTP trigger" --authlevel "ANONYMOUS"
    

    Or you can just change below line in the function code

    auth_level=func.AuthLevel.anonymous
    

    to

    auth_level=func.AuthLevel.ANONYMOUS
    

    function_app.py:

    import azure.functions as func
    import datetime
    import json
    import logging
    
    app = func.FunctionApp()
    
    @app.route(route="HttpExample", auth_level=func.AuthLevel.ANONYMOUS)
    def HttpExample(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')
    
        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
            )
    

    Output:

    C:\Users\uname\pyfunc> func start
    Found Python version 3.11.9 (py).
    
    Azure Functions Core Tools
    Core Tools Version:       4.0.6280 Commit hash: N/A +421f0144b42047aa289ce691dc6db4fc8b6143e6 (64-bit)
    Function Runtime Version: 4.834.3.22875
    
    [2024-12-20T14:18:44.871Z] Worker process started and initialized.
    
    Functions:
    
            HttpExample:  http://localhost:7071/api/HttpExample
    
    For detailed output, run func with --verbose flag.
    [2024-12-20T14:18:49.220Z] Executing 'Functions.HttpExample' (Reason='This function was programmatically called via the host APIs.', Id=3fb0fddd-4f96-4d7c-bd8f-b0fdee4aa3a1)
    [2024-12-20T14:18:49.408Z] Python HTTP trigger function processed a request.
    [2024-12-20T14:18:49.574Z] Executed 'Functions.HttpExample' (Succeeded, Id=3fb0fddd-4f96-4d7c-bd8f-b0fdee4aa3a1, Duration=444ms)
    [2024-12-20T14:18:50.070Z] Host lock lease acquired by instance ID '000000000000000000000000F72731CC'.
    

    enter image description here