I am trying to debug Azure functions python code using VS code IDE.
Local.settings.json is updated with below config
"AzureWebJobsStorage": "UseDevelopmentStorage=true"
Things I tried so far :-
Below is the error on VS Code IDE when trying to debug Azure function written in Python:
Host.json below
{
"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 below
{
"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": []
}
]
}
There were many workarounds to resolve ECONNREFUSED 127.0.0.1:9091
in Visual Studio Code - Stack: Python Azure Functions:
Approach 1:
Modify the tasks.json
(available in .vscode
folder in VS Code) like the below one:
Approach 2:
If you don't want to use the above modifications of task.json
in every new project whenever this error ECONNREFUSED 127.0.0.1:9091
occurs, then you can use this workaround given in the GitHub vsCode-AzureFunctions Issue No: 760.
"terminal.integrated.shell.windows"
value should be powershell.exe
.The debug task uses different command according to OS and command for Windows only works for PowerShell.
Note:
launch.json
and tasks.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?
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 intasks.json
(-m ptvsd --host 127.0.0.1 --port 9091
) andlaunch.json
. These are set to9091
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.
Approach 3:
As specified in the MSFT Q&A for the error ECONNREFUSED 127.0.0.1:9091
in Azure Functions Python, could you please explicit the Azure Functions Extension Bindings/Bundles explicitly as python is Non .NET Language and run/debug the Function.
Approach 4:
As mentioned in the GitHub - Azure Functions - Issue No 1016, two other workarounds of this kind of issue exists longback were:
- Change the PowerShell separator (
;
) to the cmd separator (&&
) in your.vscode/tasks.json
file.- Change your terminal to PowerShell. See here for more info: https://code.visualstudio.com/docs/editor/integrated-terminal#_configuration