VSCode调试Azure Python Durable Function遇ModuleNotFoundError求助
问题
严格遵循官方教程创建Python Durable Function,流程正常。虚拟环境下通过CLI可正常启动项目,但在VSCode中启动调试时,持续抛出ModuleNotFoundError,提示找不到已安装的azure.durable_functions模块。已将VSCode的Python解释器设置为虚拟环境(.env)内的版本,该问题在两台Windows机器和一台Ubuntu机器上均能复现。环境版本:Python 3.11,VSCode为最新版及1.77.3。
相关配置
requirements.txt
azure-functions azure-functions-durable
.vscode/launch.json
{ "version": "0.2.0", "configurations": [ { "name": "Attach to Python Functions", "type": "python", "request": "attach", "port": 9091, "preLaunchTask": "func: host start", } ] }
.vscode/tasks.json
{ "version": "2.0.0", "tasks": [ { "type": "func", "label": "func: host start", "command": "host start", "problemMatcher": "$func-python-watch", "isBackground": true } ] }
解决方法
1. 重新确认虚拟环境依赖安装
在VSCode终端手动激活虚拟环境:
- Windows:
.env\Scripts\activate - Ubuntu:
source .env/bin/activate
激活后重新安装依赖:
pip install -r requirements.txt
2. 指定func命令使用虚拟环境的Python解释器
修改.vscode/tasks.json,添加env配置强制指定虚拟环境路径:
{ "version": "2.0.0", "tasks": [ { "type": "func", "label": "func: host start", "command": "host start", "problemMatcher": "$func-python-watch", "isBackground": true, "env": { // Windows环境配置 "PYTHONPATH": "${workspaceFolder}\\.env\\Lib\\site-packages", "FUNCTIONS_WORKER_RUNTIME": "python", "PYTHON_EXECUTABLE": "${workspaceFolder}\\.env\\Scripts\\python.exe" // Ubuntu环境请替换为以下两行 // "PYTHONPATH": "${workspaceFolder}/.env/lib/python3.11/site-packages", // "PYTHON_EXECUTABLE": "${workspaceFolder}/.env/bin/python" } } ] }
3. 验证VSCode解释器配置
按Ctrl+Shift+P打开命令面板,输入Python: Select Interpreter,确认选中的是.env虚拟环境内的Python可执行文件。
4. 清除缓存并重启VSCode
关闭VSCode,删除项目根目录下的__pycache__文件夹,重新打开VSCode后再次尝试调试。
5. 强制绑定func命令的Python环境
在激活虚拟环境的终端中执行:
- Windows:
func settings add PYTHON_EXECUTABLE ".env\Scripts\python.exe" - Ubuntu:
func settings add PYTHON_EXECUTABLE ".env/bin/python"
执行完成后重启func host。
内容的提问来源于stack exchange,提问作者basquiatraphaeu
相关产品推荐
相关产品推荐

