Rudiger Wolf
Rudiger Wolf

Reputation: 1768

Debuging Multiple Python Azure-Functions locally in VS Code

I followed this Microsoft tutorial using CLI to create Azure-Function in python.

I then created a second function, both HttpTriggered.

C:\Users\rnwol\workspace\anotherazfunc
├── host.json
└── LocalFunctionProj
   ├── host.json
   ├── HttpCONTACT
   |  ├── function.json
   |  ├── host.json
   |  ├── __init__.py
   |  └── __pycache__
   ├── HttpRNWOLF
   |  ├── function.json
   |  ├── __init__.py
   |  └── __pycache__
   ├── local.settings.json
   └── requirements.txt

When in a Powershell terminal I can run both of the functions as follows...

(.venv) C:\Users\rnwol\workspace\anotherazfunc\LocalFunctionProj> func host start
Found Python version 3.8.2 (python).

                  %%%%%%
                 %%%%%%
            @   %%%%%%    @
          @@   %%%%%%      @@
       @@@    %%%%%%%%%%%    @@@
     @@      %%%%%%%%%%        @@
       @@         %%%%       @@
         @@      %%%       @@
           @@    %%      @@
                %%
                %

Azure Functions Core Tools (3.0.2245 Commit hash: 1d094e2f3ef79b9a478a1621ea7ec3f93ac1910d)
Function Runtime Version: 3.0.13139.0
[15/04/2020 20:43:27] FUNCTIONS_WORKER_RUNTIME set to python. Skipping WorkerConfig for language:java
[15/04/2020 20:43:27] FUNCTIONS_WORKER_RUNTIME set to python. Skipping WorkerConfig for language:node
[15/04/2020 20:43:27] FUNCTIONS_WORKER_RUNTIME set to python. Skipping WorkerConfig for language:powershell
[15/04/2020 20:43:28] Building host: startup suppressed: 'False', configuration suppressed: 'False', startup operation id: '21466b21-c6d0-4a25-9df4-61b42b364bef'
[15/04/2020 20:43:28] Reading host configuration file 'C:\Users\rnwol\workspace\anotherazfunc\LocalFunctionProj\host.json'
[15/04/2020 20:43:28] Host configuration file read:
[15/04/2020 20:43:28] {
[15/04/2020 20:43:28]   "version": "2.0",
[15/04/2020 20:43:28]   "extensionBundle": {
[15/04/2020 20:43:28]     "id": "Microsoft.Azure.Functions.ExtensionBundle",
[15/04/2020 20:43:28]     "version": "[1.*, 2.0.0)"
[15/04/2020 20:43:28]   }
[15/04/2020 20:43:28] }
[15/04/2020 20:43:28] Reading functions metadata
[15/04/2020 20:43:28] 2 functions found
...

I can use httpie to trigger both functions as required.

THE PROBLEM - How to interactively debug in VS-Code?

When I now open the files in VS Code, for a single function, I can then press F5 to run the command to debug and step through the Trigger event and figure out what the problem is.

What configuration should I use to run both the functions such that I can set break point and debug interactively in VS-Code? I am using Windows 10.

Direction for solution

I have configured a sample template github repo https://github.com/rnwolf/azure-func-python-layout with the results of my research, which you could use as a starter for your Azure Functions.

The layout that works for me is below. Make sure to open VSCode in the root directory, multi3, in the example below. I have found you can delete the .vscode and the .vstest and then VSCode seems to figure out how to setup some valid defaults.

(.venv) ...orkspace/multi3$ tree -L 3 -a
.
├── .coverage
├── .coveragerc
├── .flake8
├── .git
├── .github
│   └── workflows
│       └── main.yml
├── .gitignore
├── .hypothesis
├── .pre-commit-config.yaml
├── .pylintrc
├── .python-version
├── .venv
│   ├── COPYING
│   ├── COPYING.GPL
│   ├── bin
│   │   ├── Activate.ps1
│   │   ├── activate
│   │   ├── activate.csh
│   │   ├── activate.fish
│   │   ├── autopep8-wrapper
│   │   ├── bandit
│   │   ├── bandit-baseline
│   │   ├── bandit-config-generator
│   │   ├── black
│   │   ├── blackd
│   │   ├── chardetect
│   │   ├── check-added-large-files
│   │   ├── check-ast
│   │   ├── check-builtin-literals
│   │   ├── check-byte-order-marker
│   │   ├── check-case-conflict
│   │   ├── check-docstring-first
│   │   ├── check-executables-have-shebangs
│   │   ├── check-json
│   │   ├── check-merge-conflict
│   │   ├── check-symlinks
│   │   ├── check-toml
│   │   ├── check-vcs-permalinks
│   │   ├── check-xml
│   │   ├── check-yaml
│   │   ├── coverage
│   │   ├── coverage-3.8
│   │   ├── coverage3
│   │   ├── debug-statement-hook
│   │   ├── detect-aws-credentials
│   │   ├── detect-private-key
│   │   ├── dmypy
│   │   ├── double-quote-string-fixer
│   │   ├── easy_install
│   │   ├── easy_install-3.8
│   │   ├── end-of-file-fixer
│   │   ├── epylint
│   │   ├── file-contents-sorter
│   │   ├── fix-encoding-pragma
│   │   ├── flake8
│   │   ├── forbid-new-submodules
│   │   ├── http
│   │   ├── https
│   │   ├── identify-cli
│   │   ├── isort
│   │   ├── mixed-line-ending
│   │   ├── mypy
│   │   ├── mypyc
│   │   ├── name-tests-test
│   │   ├── no-commit-to-branch
│   │   ├── nodeenv
│   │   ├── pbr
│   │   ├── pip
│   │   ├── pip-compile
│   │   ├── pip-sync
│   │   ├── pip3
│   │   ├── pip3.8
│   │   ├── pipenv
│   │   ├── pipenv-resolver
│   │   ├── pre-commit
│   │   ├── pre-commit-validate-config
│   │   ├── pre-commit-validate-manifest
│   │   ├── pretty-format-json
│   │   ├── py.test
│   │   ├── pycodestyle
│   │   ├── pydocstyle
│   │   ├── pyflakes
│   │   ├── pygmentize
│   │   ├── pylint
│   │   ├── pyreverse
│   │   ├── pytest
│   │   ├── python -> /home/rnwolf/.pyenv/versions/3.8.2/bin/python
│   │   ├── python3 -> python
│   │   ├── requirements-txt-fixer
│   │   ├── safety
│   │   ├── sort-simple-yaml
│   │   ├── stubgen
│   │   ├── stubtest
│   │   ├── symilar
│   │   ├── trailing-whitespace-fixer
│   │   ├── virtualenv
│   │   └── virtualenv-clone
│   ├── include
│   ├── lib
│   │   └── python3.8
│   ├── lib64 -> lib
│   └── pyvenv.cfg
├── .vscode
│   ├── .ropeproject
│   │   └── config.py
│   ├── extensions.json
│   ├── launch.json
│   ├── settings.json
│   └── tasks.json
├── .vstest
│   ├── extensions.json
│   ├── launch.json
│   ├── settings.json
│   └── tasks.json
├── Create .venv pythonvirtual env here.txt
├── LICENSE.md
├── README.md
├── __app__
│   ├── .funcignore
│   ├── .python_packages
│   ├── __init__.py
│   ├── conftest.py
│   ├── host.json
│   ├── http_trigger_1
│   │   ├── __init__.py
│   │   ├── __pycache__
│   │   └── function.json
│   ├── http_trigger_2
│   │   ├── __init__.py
│   │   ├── __pycache__
│   │   └── function.json
│   ├── local.settings.SAMPLE.json
│   ├── requirements.txt
│   └── sharedcode
│       ├── __init__.py
│       ├── __pycache__
│       └── my_helper_functions.py
├── dev-requirements.in
├── dev-requirements.txt
├── mypy.ini
├── pytest.ini
└── tests
    ├── .pylintrc
    ├── __init__.py
    ├── testHttpTrigger1.http
    ├── testHttpTrigger2.http
    ├── test_http_trigger_1.py
    └── test_http_trigger_2.py

Upvotes: 2

Views: 4138

Answers (1)

suziki
suziki

Reputation: 14093

In your case, running f5 will starts two functions. You need to send a request to the corresponding URL to go into the logic when you debug these two functions. When you use f5 at the functionapp level, all triggers will start and are in a state of waiting for trigger.

You can create some breakpoints within the trigger logic that you need to debug, and then satisfy its triggering method.

enter image description here

For example, if you have two http trigger, and you want to debug httptrigger1. You can create some breakpoint in it and then run f5. After that, send a request to the related url and then use f8(continue) to debug.

This is my launch.json:

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

This is the settings.json:

{
    "azureFunctions.deploySubpath": ".",
    "azureFunctions.scmDoBuildDuringDeployment": true,
    "azureFunctions.pythonVenv": ".venv",
    "azureFunctions.projectLanguage": "Python",
    "azureFunctions.projectRuntime": "~2",
    "debug.internalConsoleOptions": "neverOpen",
    "python.pythonPath": ".venv\\Scripts\\python.exe"
}

And this is my tasks.json:

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "func",
            "command": "host start",
            "problemMatcher": "$func-watch",
            "isBackground": true,
            "dependsOn": "pipInstall"
        },
        {
            "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": []
        }
    ]
}

Upvotes: 4

Related Questions