我使用 Azure 文档中的以下 CLI 命令在 Windows 上创建了本地 Azure 函数项目:
func init --python
func new --name HttpExample --template "HTTP trigger" --authlevel "anonymous"
使用
func start
命令启动该函数后,工作程序退出并出现以下错误:
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
我该如何解决这个问题?
我尝试过的:
由于 func new
命令中提供的
无效的 authlevel,您收到错误。
您应该将命令中的anonymous更改为ANONYMOUS。
更改命令如下:
func new --name HttpExample --template "HTTP trigger" --authlevel "ANONYMOUS"
或者您可以更改功能代码中的以下行
auth_level=func.AuthLevel.anonymous
到
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
)
输出:
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'.