Shivani
Shivani

Reputation: 216

VS Code - How to add multiple folder paths to python path for Azure Function Apps?

I am working with Python V2 Azure Function Apps with Visual Studio Code, this is an API project hence in the main folder, I have separate folders for specific sections of API. My project structure is as below -

UI-API-Project 
    |- commonmodules (submodule)
    |- .vscode
          |- tasks.json
          |- launch.json
          |- settings.json
          |- extensions.json
    |- DashboardAPIProject
          |- function_app.py
          |- host.json
          |- local.settings.json
          |- requirements.txt
    |- UploadAPIProject
          |- function_app.py
          |- host.json
          |- local.settings.json
          |- requirements.txt

My configuration files contents are such -

{
    "configurations": [
        {
             "name": "Attach to Dashboard,
             "type": "python",
             "request": "attach",
             "port": 9091,
             "preLaunchTask": "func: host start",
         }
    }

tasks.json

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

settings.json

{
    "azureFunctions.deploySubpath": "API.Dashboard",
    "azureFunctions.scmDoBuildDuringDeployment": true,
    "azureFunctions.pythonVenv": ".venv",
    "azureFunctions.projectLanguage": "Python",
    "azureFunctions.projectRuntime": "~4",
    "debug.internalConsoleOptions": "neverOpen",
    "azureFunctions.projectLanguageModel": 2,
    "azureFunctions.projectSubpath": "API.Dashboard"
}

The problem I have is that when I specify a particular project in settings.json, only that debugs, not all endpoints from both projects are specified. Can anyone help me with a way where I can add both folders (& many more in upcoming days) without having to change config everytime & only one runs at a moment.

Upvotes: 0

Views: 448

Answers (1)

Ikhtesam Afrin
Ikhtesam Afrin

Reputation: 6469

As per the documentation, it is recommended to add all the triggers of azure functions for v2 python programming model in function_app.py file to reduce the complexity.

  • I have created two function app in vs code and my folder structure looks like below.

enter image description here

  • I made few changes to the task.json file, which is shown below-
{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "func",
            "label": "func: host start",
            "command": "host start",
            "problemMatcher": "$func-python-watch",
            "isBackground": true,
            "dependsOn": "pip install (functions)",
            "options": {  
                "cwd": "${workspaceFolder}/${input:functionapp}"  
         } 
        },
        {
            "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": [],
            "options": {  
                "cwd": "${workspaceFolder}/${input:functionapp}"  
           } 
        }
    ],
    "inputs": [  
       {  
         "id": "functionapp",  
         "description": "Which Function App?",  
         "type": "pickString",  
         "options": [  
           "DashboardAPIProject",  
           "UploadAPIProject"  
         ]  
       }  
     ] 
}
  • When I debug or clicked fn + f5, I got the given folder options to choose among them in order to execute a function in it. In this way, you don't need to change the folder name every time and also, you can get to add more folders to it.
  • But in this way, you can run one folder's endpoint at once.

enter image description here

enter image description here

References-

How to run/debug Azure Functions project in a sub-folder in VS Code? - Microsoft Q&A.

Upvotes: 0

Related Questions