VS Code中如何为Python V2 Azure Function Apps配置多项目切换调试
多Python V2 Azure Function项目在VS Code中便捷切换调试方案
项目背景
主目录UI-API-Project结构:
commonmodules/:公共子模块DashboardAPIProject/:独立Function项目,包含function_app.py、host.json等配置文件UploadAPIProject/:另一个独立Function项目,配置文件齐全
当前痛点:通过settings.json固定单个项目后,切换调试需要频繁修改配置,且要保证commonmodules能被正确引用。
1. 配置多调试启动项(launch.json)
修改launch.json,为每个Function项目添加独立调试配置,指定不同端口和对应前置任务:
{ "configurations": [ { "name": "Attach to Dashboard", "type": "python", "request": "attach", "port": 9091, "preLaunchTask": "func: host start - Dashboard" }, { "name": "Attach to Upload", "type": "python", "request": "attach", "port": 9092, "preLaunchTask": "func: host start - Upload" } ] }
2. 配置多任务(tasks.json)
为每个项目创建独立的func host start任务和依赖的pip安装任务,指定对应项目的工作目录:
{ "version": "2.0.0", "tasks": [ // Dashboard项目相关任务 { "type": "func", "label": "func: host start - Dashboard", "command": "host start", "problemMatcher": "$func-python-watch", "isBackground": true, "dependsOn": "pip install (functions - Dashboard)", "options": { "cwd": "${workspaceRoot}/DashboardAPIProject" } }, { "label": "pip install (functions - Dashboard)", "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}/DashboardAPIProject" } }, // Upload项目相关任务 { "type": "func", "label": "func: host start - Upload", "command": "host start", "problemMatcher": "$func-python-watch", "isBackground": true, "dependsOn": "pip install (functions - Upload)", "options": { "cwd": "${workspaceRoot}/UploadAPIProject" } }, { "label": "pip install (functions - Upload)", "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}/UploadAPIProject" } } ] }
3. 确保commonmodules引用正常
两种方法任选其一:
- 方法一:在每个项目的
requirements.txt末尾添加相对路径引用:-e ../commonmodules - 方法二:在每个项目根目录创建
.env文件,添加Python路径配置:PYTHONPATH=../commonmodules
4. 便捷切换调试
在VS Code左侧「运行和调试」面板,直接选择对应的启动项(比如Attach to Dashboard或Attach to Upload),点击启动按钮即可自动启动对应项目的Function Host并附加调试,全程无需手动修改配置。
内容的提问来源于stack exchange,提问作者Shivani
相关产品推荐
相关产品推荐

