【问题标题】:Unable to debug Python Azure function in VS Code IDE. Getting connect ECONNREFUSED 127.0.0.1:9091 error无法在 VS Code IDE 中调试 Python Azure 函数。获取连接 ECONNREFUSED 127.0.0.1:9091 错误
【发布时间】:2023-02-19 06:39:12
【问题描述】:

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

"AzureWebJobsStorage": "UseDevelopmentStorage=true"

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

  • 我重新安装了 VS 代码,
  • 将 Azure Function Core Tools 从 4.0 降级到 3.0
  • 解决此问题的任何指示都将非常有帮助。

以下是尝试调试用 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"
        }
    ]
}

任务.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-code azure-functions azure-functions-runtime azure-functions-core-tools azurite


    【解决方案1】:

    在 Visual Studio Code 中有许多解决方法可以解决 ECONNREFUSED 127.0.0.1:9091 - 堆栈:Python Azure 函数:

    方法一:

    修改 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. 键入用户设置并选择:转到用户菜单中的功能 > 选择终端 > 进行设置 "terminal.integrated.shell.windows" 值应为 powershell.exe

      调试任务根据操作系统使用不同的命令,Windows 的命令仅适用于 PowerShell。

      笔记:

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

      当主机以 Python 调试功能启动时,有两个不同的端口在工作: . 7071 是主机公开 HTTP 端点的默认端口。

      这套在哪里?

      1. 9091 是用于启动 Python worker 调试端点的默认端口。这是远程连接到 worker 所必需的。这需要在tasks.json (-m ptvsd --host 127.0.0.1 --port 9091) 和launch.json 中相同。这些设置为9091 这两者都需要彼此区分,但除此之外,它们具有什么值并不重要。这些设置应该由 VSCode 函数创建体验来处理,这样就不会出现冲突。

        方法三:

        正如在 MSFT Q&A 中针对 Azure Functions Python 中的错误 ECONNREFUSED 127.0.0.1:9091 所指定的那样,您能否明确显示 Azure Functions 扩展绑定/捆绑包,因为 python 是非 .NET 语言并运行/调试函数。

        方法四:

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

    【讨论】:

    • 累人的选项 2、3 和方法 4 已经设置,它只在第一次工作。新的 VS 代码会话触发相同的错误。除了尝试检查 netstat -p tcp -ano | findstr 127.0.0.1:9091 命令,没有进程正在运行 9091。请帮助我们解决此问题
    • 尝试过方法 1,它不起作用。
    • 您可以尝试在host.json 中使用不同的端口设置吗?
    • 下面是我的 host.json 设置和 launch.json 设置}}, "extensionBundle": {"id": Microsoft.Azure.Functions.ExtensionBundle","version": "[3.3.0, 4.0.0)" }, "functionTimeout": "20:00:00", "extensions": {"durableTask": { "maxConcurrentActivityFunctions": 1 } }} { "version": "0.2.0", "configurations": [ { "name": "Attach to Python Functions", "type": "python", "request": "attach", "port": 9091, "preLaunchTask": "func: host start" } ]}
    • 尝试更改 launch.json 中的不同端口,但没有成功
    【解决方案2】:

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

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

    以下帖子有一些关于在哪里可以找到扩展的说明(取决于操作系统)。

    Completely uninstall VS Code extensions

    【讨论】:

      【解决方案3】:

      我在 VSC 调试器中遇到同样的问题,每次运行调试器时都会出现 ECONNREFUSED 127.0.0.1(我是新手!)。经过大量的试验和错误(也是上述技巧)后,我找到了重置错误情况的解决方案,这只是刷新调试器窗口:

      Screenshot: Click here to refresh

      当您单击刷新时,错误消失了! :-)

      Screenshot: Error gone after clicking refresh!

      【讨论】:

        猜你喜欢
        • 2021-10-18
        • 2020-02-12
        • 2019-03-15
        • 2021-03-14
        • 1970-01-01
        • 1970-01-01
        • 2022-01-15
        • 2019-08-07
        • 2018-12-11
        相关资源
        最近更新 更多