Azure Functions Python 错误 - 没有作业函数

问题描述 投票:0回答:4

我尝试使用 python 在本地运行我的 Azure Functions,收到的错误消息如下:

[2023-03-03T11:34:59.832Z] No job functions found. Try making your job classes and methods public. If you're using binding extensions (e.g. Azure Storage, ServiceBus, Timers, etc.) make sure you've called the registration method for the extension(s) in your startup 
code (e.g. builder.AddAzureStorage(), builder.AddServiceBus(), builder.AddTimers(), etc.).

我使用此命令创建了我的项目,并且没有更改生成的代码中的任何内容:

func init LocalFunctionProj --python -m V2

当我尝试使用此命令运行应用程序时,我收到上述错误消息:

func start --verbose 

我在 python v3.10 上运行它。另外,当我尝试创建 V1 python 函数时,它工作没有任何问题..

有什么想法为什么会发生这种情况吗?

python azure azure-functions
4个回答
8
投票

就我而言,我在 local.settings.json 中缺少一行

“AzureWebJobsFeatureFlags”:“启用WorkerIndexing”

与文档相比这里

// local.settings.json
{
  "IsEncrypted": false,
  "Values": {
    "FUNCTIONS_WORKER_RUNTIME": "python",
    "STORAGE_CONNECTION_STRING": "<AZURE_STORAGE_CONNECTION_STRING>",
    "AzureWebJobsStorage": "<azure-storage-connection-string>",
    "AzureWebJobsFeatureFlags": "EnableWorkerIndexing"
  }
}

4
投票

使用相同的命令,我在本地环境中创建并测试了 - Python 3.10.10 v2 模型 Azure Functions 项目:

enter image description here

在终端中,使用

cd .\LocalFunctionProj\
更改路径来运行函数
func start --verbose
enter image description here

我为 Python v2 模型提供了在本地和 Azure 云中工作的实用解决方案 - SO #74671905

错误原因可能是任何代码文件中的典型代码错误,例如

function_app.py
local.settings.json
host.json
,或者检查 Azurite 模拟器是否安装在 VS Code 扩展中并处于运行状态。

代码片段function_app.py

import azure.functions as  func
app = func.FunctionApp()

@app.function_name(name="HttpTrigger1")
@app.route(route="hello") # HTTP Trigger
def  test_function(req: func.HttpRequest) -> func.HttpResponse:
return  func.HttpResponse("HttpTrigger1 function processed a request!!!")

需求.txt

azure-functions

local.settings.json:

{
"IsEncrypted": false,
"Values": {
"FUNCTIONS_WORKER_RUNTIME": "python",
"AzureWebJobsFeatureFlags": "EnableWorkerIndexing",
"AzureWebJobsStorage": "UseDevelopmentStorage=true"
}
}

主机.json

{
"version": "2.0",
"logging": {
"applicationInsights": {
    "samplingSettings": {
    "isEnabled": true,
    "excludedTypes": "Request"
    }
}
},
"extensionBundle": {
    "id": "Microsoft.Azure.Functions.ExtensionBundle",
    "version": "[3.15.0, 4.0.0)"
    }
}

如果所有代码和配置都正确,请尝试激活虚拟环境(Python文章),然后运行该函数:

py -m pip install --user virtualenv
py -m venv env
.\env\Scripts\activate
func start --verbose

0
投票

无论如何,我收到此错误是因为我安装了旧版本的 Azure Function Tools,不支持 v2 Python 函数模型。我使用 VS Code 模板而不是 CLI 创建了函数。为了解决这个问题,我只是升级了我的 Functions Core Tools。在 Mac 上:

$ brew upgrade azure-functions-core-tools@4

我做的另一件事——我认为这实际上没有什么区别——是用函数名称注释我的 Python 函数,并将访问级别从

FUNCTION
更改为
ANONYMOUS
,这两者都来自来自 Microsoft 的文档。开箱即用的代码有:

app = func.FunctionApp(http_auth_level=func.AuthLevel.FUNCTION)

@app.route(route="myFunc")
def myFunc(req: func.HttpRequest) -> func.HttpResponse:
  ...

但我把它改为:

app = func.FunctionApp(http_auth_level=func.AuthLevel.ANONYMOUS)

@app.function_name("MyFunc")
@app.route(route="myFunc")
def my_func(req: func.HttpRequest) -> func.HttpResponse:
  ...

此外,在开始我的函数之前,我必须确保 Azurite(Azure 存储模拟器)正在运行。确保安装了 VS Code 扩展,然后运行“Azurite: Start”。

希望能帮助其他像我一样被困在这个问题上一段时间的人!


0
投票

我的 azure 函数也遇到同样的问题,解决方案是添加:

"AzureWebJobsFeatureFlags": "EnableWorkerIndexing"

到我的local.settings.json,因为你正在本地调试。 感谢@Yasufuk

© www.soinside.com 2019 - 2024. All rights reserved.