Python Azure Function(V2)启动报错:无法加载System.Net.Http.Formatting
问题排查与解决方案
1. 修复代码中的变量错误
你的代码存在两处变量问题,会直接导致运行时异常甚至影响启动:
- 路由参数定义为
{Id},但代码中错误尝试获取companyId,应改为req.route_params.get('Id') logging.info(company_id)中的company_id未定义,需替换为声明的site_id,同时补充必要的返回逻辑避免函数无返回报错
修正后的代码:
import logging import azure.functions as func app = func.FunctionApp() @app.function_name(name="GetSettings") @app.route(route="company/{Id}/Settings", methods=['GET'], auth_level=func.AuthLevel.FUNCTION) def GetSettings(req: func.HttpRequest) -> func.HttpResponse: logging.info('Python HTTP trigger function processed a request.') # Get ID from the route parameter site_id = req.route_params.get('Id') logging.info(site_id) if not site_id: return func.HttpResponse("缺少Id参数", status_code=400) return func.HttpResponse(f"获取到的Id: {site_id}", status_code=200)
2. 解决.NET依赖缺失问题
报错提到的System.Net.Http.Formatting是.NET框架组件,该问题通常和Azure Functions CLI版本、本地运行时配置有关:
- 更新Azure Functions Core Tools到最新稳定版:执行
npm install -g azure-functions-core-tools@4(需提前安装Node.js) - 检查
local.settings.json中的FUNCTIONS_WORKER_RUNTIME是否设置为python,避免误配置为.NET相关值 - 清理本地运行缓存:删除项目根目录下的
.azurefunctions和__pycache__文件夹,重新执行func host start
3. 确认Python依赖配置
确保requirements.txt包含正确的Azure Functions依赖,内容至少包含:
azure-functions>=1.17.0 requests>=2.31.0
执行pip install -r requirements.txt,确保依赖安装在Python3.9环境中(建议使用虚拟环境隔离版本)
4. 验证Python解释器配置
在VS Code中确认已选择Python3.9作为项目解释器:
- 按下
Ctrl+Shift+P(Windows)或Cmd+Shift+P(Mac),输入Python: Select Interpreter,选择本地安装的Python3.9环境
内容的提问来源于stack exchange,提问作者Shivani
相关产品推荐
相关产品推荐

