如何在VSCode中调试多个Python V2版Azure Functions项目
问题诊断与修复方案
核心问题
- 任务配置仅指向单一项目:
tasks.json里的func: host start和pip install任务默认绑定API.Preferences目录,第二个调试配置调用该任务时,实际启动的还是第一个项目的函数宿主。 - 调试配置的预启动任务不匹配:两个调试项共用同一个启动任务,无法分别触发对应项目的宿主启动流程。
- 全局设置存在固定项目路径:
settings.json中azureFunctions.projectSubpath固定为API.Charts,会干扰多项目的环境识别逻辑。
分步修复
1. 更新tasks.json,添加第二个项目的专属任务
为API.Charts创建独立的启动和依赖安装任务,确保每个调试配置能启动对应项目的函数宿主:
{ "version": "2.0.0", "tasks": [ // API.Preferences 专属任务 { "type": "func", "label": "func: host start (Preferences)", "command": "host start", "problemMatcher": "$func-python-watch", "isBackground": true, "dependsOn": "pip install (Preferences)", "options": { "cwd": "${workspaceFolder}/API.Preferences" } }, { "label": "pip install (Preferences)", "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}/API.Preferences" } }, // API.Charts 专属任务 { "type": "func", "label": "func: host start (Charts)", "command": "host start", "problemMatcher": "$func-python-watch", "isBackground": true, "dependsOn": "pip install (Charts)", "options": { "cwd": "${workspaceFolder}/API.Charts" } }, { "label": "pip install (Charts)", "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}/API.Charts" } } ] }
2. 修改launch.json,绑定对应项目的预启动任务
让每个调试配置调用专属的启动任务,确保启动正确的函数宿主:
{ "configurations": [ { "name": "Attach to Preferences", "type": "python", "request": "attach", "port": 9091, "preLaunchTask": "func: host start (Preferences)", "project": "${workspaceFolder}/API.Preferences" }, { "name": "Attach to Charts", "type": "python", "request": "attach", "port": 7071, "preLaunchTask": "func: host start (Charts)", "project": "${workspaceFolder}/API.Charts" } ] }
3. 调整settings.json,移除全局项目路径限制
删除azureFunctions.projectSubpath配置,避免固定单一项目导致多项目识别异常:
{ "azureFunctions.deploySubpath": ".", "azureFunctions.scmDoBuildDuringDeployment": true, "azureFunctions.pythonVenv": ".venv", "azureFunctions.projectLanguage": "Python", "azureFunctions.projectRuntime": "~4", "debug.internalConsoleOptions": "neverOpen", "azureFunctions.projectLanguageModel": 2 }
4. 验证工作区配置(可选)
确保code.workspace.json中的复合调试配置能正常关联修改后的调试项:
{ "folders": [ { "path": ".." }, { "path": "../API.Preferences" }, { "path": "../API.Charts" } ], "settings": { "debug.internalConsoleOptions": "neverOpen" }, "launch": { "configurations": [], "compounds": [ { "name": "Attach to both apps", "configurations": [ "Attach to Preferences", "Attach to Charts" ] } ] } }
调试验证
- 分别选择
Attach to Preferences和Attach to Charts启动调试,确认对应项目的函数宿主正常启动并成功附加调试器。 - 若需要同时调试两个项目,选择复合配置
Attach to both apps即可。
内容的提问来源于stack exchange,提问作者Shivani
相关产品推荐
相关产品推荐

