admin管理员组

文章数量:1391975

How do I get debugger to run on my function in VSCode with this as my launch.json and task.json (respectively) when I use type shell on a task?

launch.json

task.json

When I run debugger on this, I get the "connect ECONNREFUSED 127.0.0.1:9091" error.

When I run debugger with type "func" it works as normal and it stops at breakpoints and terminates without errors.

task.json

The only difference between functional and "connection refused" is having the type as "func" or type as "shell" in my tasks.json.

Is it possible that a type "shell" task has a different default port?

Any suggestions to fix this is appreciated, thank you!


Goal of what I'm trying to do...:

I've created an Azure function in Python on VSCode. It works and the debugger worked as well after I added a .vscode folder. I need a virtual environment so I can ensure consistency between me and other people or setups which could be working on this. To do this, I use pipenv. "pipenv shell" and "pipenv run func host start" both work fine when I use powershell/cmd terminal in VSCode. The problem is when I try to use the VSCode debugger on this.

I was trying to use a type "shell" task with the command being "pipenv run func host start" when I stumbled on this error with the debugger. This command works when I enter it into the vscode terminal(powershell). When I put this into task.json however, it gave me a connection refused error on the port. As stated above, this works fine when I use type "func" instead of "shell", however a type "func" does not allow me to run pipenv.

After some tests and reading stack overflow solutions, I narrowed this connection refusal to just the change between type "func" or "shell".

How do I get debugger to run on my function in VSCode with this as my launch.json and task.json (respectively) when I use type shell on a task?

launch.json

task.json

When I run debugger on this, I get the "connect ECONNREFUSED 127.0.0.1:9091" error.

When I run debugger with type "func" it works as normal and it stops at breakpoints and terminates without errors.

task.json

The only difference between functional and "connection refused" is having the type as "func" or type as "shell" in my tasks.json.

Is it possible that a type "shell" task has a different default port?

Any suggestions to fix this is appreciated, thank you!


Goal of what I'm trying to do...:

I've created an Azure function in Python on VSCode. It works and the debugger worked as well after I added a .vscode folder. I need a virtual environment so I can ensure consistency between me and other people or setups which could be working on this. To do this, I use pipenv. "pipenv shell" and "pipenv run func host start" both work fine when I use powershell/cmd terminal in VSCode. The problem is when I try to use the VSCode debugger on this.

I was trying to use a type "shell" task with the command being "pipenv run func host start" when I stumbled on this error with the debugger. This command works when I enter it into the vscode terminal(powershell). When I put this into task.json however, it gave me a connection refused error on the port. As stated above, this works fine when I use type "func" instead of "shell", however a type "func" does not allow me to run pipenv.

After some tests and reading stack overflow solutions, I narrowed this connection refusal to just the change between type "func" or "shell".

Share Improve this question edited Mar 13 at 13:41 kimbers asked Mar 13 at 6:03 kimberskimbers 11 silver badge2 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Use below task.json to debug Python Azure functions:

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "func",
            "label": "func: host start",
            "command": "host start",
            "problemMatcher": "$func-python-watch",
            "isBackground": true,
            "dependsOn": "pip install (functions)"
        },
        {
            "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": []
        }
    ]
}

launch.json:

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

Click on Run and Debug in Visual Studio Code=> Attach to Python Azure functions to start debugging the Azure function:

Debug response:

 *  Executing task: .venv\Scripts\python -m pip install -r requirements.txt 

Requirement already satisfied: azure-functions in c:\users\uname\pyfn13\.venv\lib\site-packages (from -r requirements.txt (line 5)) (1.21.3)

[notice] A new release of pip is available: 24.0 -> 25.0.1
[notice] To update, run: python.exe -m pip install --upgrade pip
 *  Terminal will be reused by tasks, press any key to close it. 

 *  Executing task: .venv\Scripts\activate && func host start 

Found Python version 3.11.9 (py).

Azure Functions Core Tools
Core Tools Version:       4.0.6821 Commit hash: N/A +c09a2033faa7ecf51b3773308283af0ca9a99f83 (64-bit)
Function Runtime Version: 4.1036.1.23224

[2025-03-13T12:28:13.772Z] 0.02s - Debugger warning: It seems that frozen modules are being used, which may
[2025-03-13T12:28:13.778Z] 0.00s - make the debugger miss breakpoints. Please pass -Xfrozen_modules=off
[2025-03-13T12:28:13.780Z] 0.00s - to python to disable frozen modules.
[2025-03-13T12:28:13.781Z] 0.00s - Note: Debugging will proceed. Set PYDEVD_DISABLE_FILE_VALIDATION=1 to disable this validation.
[2025-03-13T12:28:13.879Z] Worker process started and initialized.

Functions:

        HttpTrigger1: [GET,POST] http://localhost:7071/api/HttpTrigger1

For detailed output, run func with --verbose flag.
[2025-03-13T12:28:17.524Z] Executing 'Functions.HttpTrigger1' (Reason='This function was programmatically called via the host APIs.', Id=5769278d-cd54-4cf2-952d-03284d8bf247)
[2025-03-13T12:28:17.631Z] Python HTTP trigger function processed a request.
[2025-03-13T12:28:17.695Z] Executed 'Functions.HttpTrigger1' (Succeeded, Id=5769278d-cd54-4cf2-952d-03284d8bf247, Duration=205ms)
[2025-03-13T12:28:18.808Z] Host lock lease acquired by instance ID '000000000000000000000000F72731CC'.

本文标签: