无法在 VS Code IDE 中调试 Python Azure 函数。获取连接 ECONNREFUSED 127.0.0.1:9091 错误

Unable to debug Python Azure function in VS Code IDE. Getting connect ECONNREFUSED 127.0.0.1:9091 error

我正在尝试使用 VS 代码 IDE 调试 Azure 函数 python 代码 IDE。
Local.settings.json 更新为以下配置

"AzureWebJobsStorage": "UseDevelopmentStorage=true"

到目前为止我尝试过的事情:-

下面是尝试调试用 Python 编写的 Azure 函数时 VS Code IDE 上的错误:

Host.json下方

{
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    }
  },
  "extensionBundle": {
    "id": "Microsoft.Azure.Functions.ExtensionBundle",
    "version": "[2.*, 3.0.0)"
  },
  "functionTimeout": "20:00:00",
  "extensions": {
    "durableTask": {
      "maxConcurrentActivityFunctions": 1
    }
  }
}

launch.json下方

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Attach to Python Functions",
            "type": "python",
            "request": "attach",
            "port": 9091,
            "preLaunchTask": "func: host start"
        }
    ]
}

task.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "cmd host start",
            "type": "shell",
            "dependsOn": "pip install (functions)",
            "windows": {
                "command": ". ${config:azureFunctions.pythonVenv}\Scripts\activate && func host start --verbose"
            },
            "isBackground": true,
            "problemMatcher": "$func-python-watch"
        },
        {
            "label": "pipInstall",
            "type": "shell",
            "osx": {
                "command": "${config:azureFunctions.pythonVenv}/bin/python -m pip install -r requirements.txt"
            },
            "windows": {
                "command": ". ${config:azureFunctions.pythonVenv}\Scripts\python -m pip install -r requirements.txt"
            },
            "linux": {
                "command": "${config:azureFunctions.pythonVenv}/bin/python -m pip install -r requirements.txt"
            },
            "problemMatcher": []
        },
        {
            "type": "func",
            "command": "host start",
            "problemMatcher": "$func-python-watch",
            "isBackground": true,
            "dependsOn": "func: extensions install"
        },
        
        {
            "type": "func",
            "command": "extensions install",
            "dependsOn": "pip install (functions)",
            "problemMatcher": []
        },
        {
            "label": "pip install (functions)",
            "type": "shell",
            "osx": {
                "command": "${config:azureFunctions.pythonVenv}/bin/python -m pip install -r requirements.txt"
            },
            "windows": {
                "command": ". ${config:azureFunctions.pythonVenv}\Scripts\python -m pip install -r requirements.txt"
            },
            "linux": {
                "command": "${config:azureFunctions.pythonVenv}/bin/python -m pip install -r requirements.txt"
            },
            "problemMatcher": []
        }
        
    ]
}

在 Visual Studio 代码中有许多解决方法可以解决 ECONNREFUSED 127.0.0.1:9091 - 堆栈:Python Azure Functions:

方法一:

修改 tasks.json(在 VS Code 的 .vscode 文件夹中可用),如下所示:


方法二:

如果您不想在每次出现此错误 ECONNREFUSED 127.0.0.1:9091 时在每个新项目中使用上述对 task.json 的修改,那么您可以使用 GitHub vsCode-AzureFunctions Issue No: 760 中给出的解决方法.

  1. VS Code > 你的 Python Azure Functions 项目 > 查看菜单 > 打开命令面板或 Ctrl + Shift + P。
  2. 键入用户设置和 Select:转到用户菜单中的功能 > Select 终端 > 进行设置 "terminal.integrated.shell.windows" 值应为 powershell.exe

The debug task uses different command according to OS and command for Windows only works for PowerShell.

注:

  • 根据我的研究,错误消息过于笼统。不同的 Systems/Projects 相同类型(Azure Functions Python)有不同的解决方案,正如我所经历的那样。
  • 对于少数 Systems/Projects,更改端口有效。由于 7071 是在工具中执行 HTTP 函数的默认端口,而调试器需要连接到不同的端口。
  • 在更改调试端口时,launch.jsontasks.json 都应该更改。

There are two distinct ports at work when the host is started with Python debugging capabilities: . 7071 is the default port where the HTTP endpoint is exposed by the host.

Where is this set?

  1. 9091 is the default port used for starting the debugging endpoint for the Python worker. This is needed for remote attaching to the worker. This needs to be the same in tasks.json (-m ptvsd --host 127.0.0.1 --port 9091) and launch.json. These are set to 9091 Both of these need to be distinct from each other but, other than that, it doesn't matter what values they have. These settings should be handled by the VSCode function creation experience so that conflicts do not arise.

方法三:

MSFT Q&A 中针对 Azure Functions Python 中的错误 ECONNREFUSED 127.0.0.1:9091 所指定的那样,您能否将 Azure Functions 扩展 Bindings/Bundles 显式明确为 python 是非 .NET 语言和 run/debug 函数。

方法四:

GitHub - Azure Functions - Issue No 1016 中所述,longback 存在此类问题的另外两个解决方法是:

我也遇到过这个问题,尝试了HariKrishnaRajoli-MT提供的所有解决方案。对我有用的是卸载 Azure Functions 扩展和 re-install,它工作了一段时间。我仍然偶尔会遇到这个问题,不得不重复这个过程。

虽然您提到您 re-installed VS Code,但还值得一提的是,如果需要,您也应该删除所有扩展(如果安装程序没有为您删除它)

下面的 post 有一些关于在哪里可以找到扩展的说明(取决于 OS)。