Azure Functions Python错误 – 无作业函数

huangapple go评论76阅读模式
英文:

Azure Functions Python error - no job functions

问题

以下是翻译好的部分:

[2023-03-03T11:34:59.832Z] 未找到作业函数。尝试将您的作业类和方法设置为公共。如果您正在使用绑定扩展(例如 Azure 存储、ServiceBus、Timers 等),请确保在启动代码中调用扩展的注册方法(例如 builder.AddAzureStorage()、builder.AddServiceBus()、builder.AddTimers() 等)。

我使用以下命令创建了我的项目,并没有更改生成的代码:

func init LocalFunctionProj --python -m V2

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

func start --verbose

我正在使用 Python 版本 3.10 运行此应用程序。此外,当我尝试创建 V1 版本的 Python 函数时,没有出现任何问题。

您知道为什么会出现这种情况吗?

英文:

I am trying to run locally my Azure Functions using python and the error message I get is the following:

[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.).

I created my project with this command and did not change anything in the generated code:

func init LocalFunctionProj --python -m V2

When I try to run the app with this command then I get the error message mentioned above:

func start --verbose 

I am running this on python v3.10. Also, when I tried to create the V1 python function it worked without any problems..

Any ideas why this is happening?

答案1

得分: 2

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

在终端中,使用 cd .\LocalFunctionProj\ 更改路径以运行函数 func start --verbose
Azure Functions Python错误 – 无作业函数

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

你的错误可能是代码文件中的典型代码错误之一,如 function_app.pylocal.settings.jsonhost.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触发器
def  test_function(req: func.HttpRequest) -> func.HttpResponse:
    return func.HttpResponse("HttpTrigger1 function processed a request!!!")

requirements.txt

azure-functions

local.settings.json:

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

host.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
英文:

Using the same commands, I have created and tested in my local environment - Python 3.10.10 v2 model Azure Functions Project:

Azure Functions Python错误 – 无作业函数

In the terminal, changed the path using cd .\LocalFunctionProj\ for running the function func start --verbose
Azure Functions Python错误 – 无作业函数

I have provided the practical solution working both locally and in Azure Cloud for the Python v2 model - SO #74671905

Cause of your error might be a typical code mistake in any of the code files such as function_app.py, local.settings.json, host.json, or check the Azurite emulator is installed in VS Code Extensions and in running state.

Code Snippets:
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!!!")

requirements.txt

azure-functions

local.settings.json:

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

host.json

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

If all the code and configuration is correct, try activating virtual environment (Python article) and then run the function:

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

答案2

得分: 1

In my case, I was missing a line in local.settings.json:

"AzureWebJobsFeatureFlags": "EnableWorkerIndexing"

compared to the documentation here.

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

英文:

In my case, I was missing a line in local.settings.json

> "AzureWebJobsFeatureFlags": "EnableWorkerIndexing"

compared to the documentation here.

// local.settings.json
{
  &quot;IsEncrypted&quot;: false,
  &quot;Values&quot;: {
    &quot;FUNCTIONS_WORKER_RUNTIME&quot;: &quot;python&quot;,
    &quot;STORAGE_CONNECTION_STRING&quot;: &quot;&lt;AZURE_STORAGE_CONNECTION_STRING&gt;&quot;,
    &quot;AzureWebJobsStorage&quot;: &quot;&lt;azure-storage-connection-string&gt;&quot;,
    &quot;AzureWebJobsFeatureFlags&quot;: &quot;EnableWorkerIndexing&quot;
  }
}

答案3

得分: 0

对于它值得的地方,我之所以收到此错误,是因为我安装了旧版本的 Azure Function 工具,不支持 v2 Python 函数模型。我是使用 VS Code 模板而不是 CLI 创建我的函数。要解决问题,我只需升级我的 Functions Core 工具。在 Mac 上执行以下命令:

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

我还做的另一件事(我认为实际上没有太大影响)是为我的 Python 函数添加了一个函数名称的注释,并将访问级别从 FUNCTION 更改为 ANONYMOUS,这两者均来自于Microsoft 的文档。默认的代码是这样的:

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

@app.route(route=&quot;myFunc&quot;)
def myFunc(req: func.HttpRequest) -&gt; func.HttpResponse:
  ...

但我将其更改为:

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

@app.function_name(&quot;MyFunc&quot;)
@app.route(route=&quot;myFunc&quot;)
def my_func(req: func.HttpRequest) -&gt; func.HttpResponse:
  ...

此外,我还必须确保在启动函数之前运行 Azurite(Azure 存储模拟器)。确保已安装 VS Code 扩展,然后运行 "Azurite: Start"。

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

英文:

For what it's worth, I received this error because I had an older version of Azure Function Tools installed that didn't support the v2 Python function model. I had created my function using the VS Code template rather than the CLI. To resolve, I simply upgraded my Functions Core Tools. On a Mac:

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

One other thing I did -- which I don't think actually made a difference -- was to annotate my Python function with a function name, and change the access level from FUNCTION to ANONYMOUS, both of which came from this documentation from Microsoft. The out-of-the-box code had:

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

@app.route(route=&quot;myFunc&quot;)
def myFunc(req: func.HttpRequest) -&gt; func.HttpResponse:
  ...

but I changed it to:

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

@app.function_name(&quot;MyFunc&quot;)
@app.route(route=&quot;myFunc&quot;)
def my_func(req: func.HttpRequest) -&gt; func.HttpResponse:
  ...

In addition, I had to ensure that I had Azurite (the Azure Storage emulator) running before starting my function. Ensure you have the VS Code extension installed, then run "Azurite: Start".

Hope that helps someone else who was stuck on this for a little while like I was!

huangapple
  • 本文由 发表于 2023年3月3日 20:55:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75627362.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定