Python版Azure Functions启动报错:WorkerConfig未找到及provider参数为空
Python版Azure Functions启动及依赖安装故障排查
问题概述
搭建Python版Azure Functions时,启动函数遇到WorkerConfig for runtime: python not found错误,尝试安装azure-functions-worker又出现grpcio编译失败问题。
函数代码
import azure.functions as func import logging app = func.FunctionApp(http_auth_level=func.AuthLevel.ANONYMOUS) @app.route(route="APIPreguntaleia") def APIPreguntaleia(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 )
启动时的错误日志
Executing task: .venv\Scripts\python -m pip install -r requirements.txt Requirement already satisfied: azure-functions in r:\fast-api\.venv\lib\site-packages (from -r requirements.txt (line 5)) (1.19.0) * Terminal will be reused by tasks, press any key to close it. * Executing task: .venv\Scripts\activate ; func host start Found Python version 3.11.9 (py). Azure Functions Core Tools Core Tools Version: 4.0.5801 Commit hash: N/A +5ac2f09758b98257e728dd1b5576ce5ea9ef68ff (64-bit) Function Runtime Version: 4.34.1.22669 [2024-05-30T19:29:34.890Z] A host error has occurred during startup operation '95a1dbe5-8264-4a29-b39a-5a6dc58447df'. [2024-05-30T19:29:34.893Z] Microsoft.Azure.WebJobs.Script: WorkerConfig for runtime: python not found. [2024-05-30T19:29:34.903Z] Failed to stop host instance '96a67c8f-f7a7-45fa-83c3-be938325ae8c'. [2024-05-30T19:29:34.907Z] Microsoft.Azure.WebJobs.Host: The host has not yet started. Value cannot be null. (Parameter 'provider') [2024-05-30T19:29:34.915Z] Host startup operation has been canceled * The terminal process "C:\Program Files\PowerShell\7\pwsh.exe -Command .venv\Scripts\activate ; func host start" terminated with exit code: 1. * Terminal will be reused by tasks, press any key to close it.
安装azure-functions-worker时的错误
PS R:\Fast-API> pip install azure-functions-worker Collecting azure-functions-worker Using cached azure_functions_worker-1.1.9-py3-none-any.whl.metadata (7.7 kB) Collecting grpcio~=1.33.1 (from azure-functions-worker) Using cached grpcio-1.33.2.tar.gz (20.9 MB) Installing build dependencies ... done Getting requirements to build wheel ... done Installing backend dependencies ... done Preparing metadata (pyproject.toml) ... done Collecting grpcio-tools~=1.33.1 (from azure-functions-worker) Using cached grpcio-tools-1.33.2.tar.gz (2.1 MB) Installing build dependencies ... done Getting requirements to build wheel ... done Installing backend dependencies ... done Preparing metadata (pyproject.toml) ... done Collecting six>=1.5.2 (from grpcio~=1.33.1->azure-functions-worker) Using cached six-1.16.0-py2.py3-none-any.whl.metadata (1.8 kB) Collecting protobuf<4.0dev,>=3.5.0.post1 (from grpcio-tools~=1.33.1->azure-functions-worker) Using cached protobuf-3.20.3-py2.py3-none-any.whl.metadata (720 bytes) Using cached azure_functions_worker-1.1.9-py3-none-any.whl (54 kB) Using cached protobuf-3.20.3-py2.py3-none-any.whl (162 kB) Using cached six-1.16.0-py2.py3-none-any.whl (11 kB) Building wheels for collected packages: grpcio, grpcio-tools Building wheel for grpcio (pyproject.toml) ... error error: subprocess-exited-with-error × Building wheel for grpcio (pyproject.toml) did not run successfully. │ exit code: 1 ╰─> [80 lines of output] ... error: [WinError 2] El sistema no puede encontrar el archivo especificado [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. ERROR: Failed building wheel for grpcio Building wheel for grpcio-tools (pyproject.toml) ... error error: subprocess-exited-with-error × Building wheel for grpcio-tools (pyproject.toml) did not run successfully. │ exit code: 1 ╰─> [69 lines of output] ... grpc_tools\_protoc_compiler.cpp(394): fatal error C1083: No se puede abrir el archivo incluir: 'longintrepr.h': No such file or directory error: command 'C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\bin\HostX86\x64\cl.exe' failed with exit code 2 [end of output] note: This error originates from a subprocess, and is likely not a problem with pip. ERROR: Failed building wheel for grpcio-tools Failed to build grpcio grpcio-tools ERROR: Could not build wheels for grpcio, grpcio-tools, which is required to install pyproject.toml-based projects
环境信息
- Python版本:3.11.9
- Azure Functions Core Tools版本:4.0.5801
解决方案
1. 停止手动安装azure-functions-worker
azure-functions-worker由Azure Functions Core Tools自动管理,手动安装会引发版本不兼容问题——旧版本worker依赖的grpcio版本与Python3.11不兼容(longintrepr.h在Python3.10+中已被移除)。
2. 修复WorkerConfig找不到的问题
步骤1:确认Python环境配置
激活虚拟环境后,运行:
func settings list
检查FUNCTIONS_WORKER_RUNTIME是否设为python,若未设置,执行:
func settings set FUNCTIONS_WORKER_RUNTIME python
步骤2:更新Azure Functions Core Tools
当前使用的Core Tools版本较旧,更新到最新版本:
npm install -g azure-functions-core-tools@4
若无npm,可通过微软官方渠道下载安装包更新。
步骤3:重新初始化项目(可选)
若上述步骤无效,备份代码后,删除.venv、__pycache__外的文件,重新初始化:
func init --python
将原有函数代码复制到新生成的函数目录,重新安装依赖:
python -m venv .venv .venv\Scripts\activate pip install azure-functions
3. 解决grpcio编译失败问题(若需处理)
若必须安装grpcio,使用预编译wheel包避免源码编译:
pip install grpcio==1.59.0 grpcio-tools==1.59.0 --only-binary :all:
该版本兼容Python3.11,无需本地编译。
4. 验证启动
完成上述步骤后,运行:
func host start
访问http://localhost:7071/api/APIPreguntaleia测试函数是否正常响应。
内容的提问来源于stack exchange,提问作者Sebastián Romero
相关产品推荐
相关产品推荐

