VSCode Python远程调试突然失效问题求助
pytest + debugpy 调试异常:断点随机暂停或完全不触发
问题描述
此前稳定运行两个月的pytest调试流程突然失效,原操作流程如下:
- 在测试代码或待调试代码顶层插入debugpy监听代码:
import debugpy debugpy.listen(5678) debugpy.wait_for_client()
- 在需要暂停的位置插入
debugpy.breakpoint(),或通过VSCode侧边栏标记断点(原可靠性较低) - pytest启动并显示
collecting...时按下F5附加调试
当前异常表现:
- 使用
debugpy.breakpoint()时,代码会在随机位置暂停:重装VSCode和虚拟机前停在pytest库文件随机行,重装后停在测试数据库销毁代码随机行 - 侧边栏标记的断点完全不会触发
已尝试的无效操作:
- 彻底重装VSCode
- 删除并重建Vagrant虚拟机
- 在
launch.json中添加"env": {"PYTEST_ADDOPTS": "--no-cov"}(工具提示属性不被支持,运行也无效) - 降级Python扩展
- 切换
justMyCode启动选项 - 尝试多个Git分支及仓库内不同模块
注:此前流程正常,未主动修改任何配置
环境信息
- VSCode版本:1.74.2 (Universal)
- 系统:Darwin x64 21.6.0
- 现有
launch.json配置:
{ "version": "0.2.0", "configurations": [ { "name": "Python: Remote Attach", "type": "python", "request": "attach", "connect": { "host": "localhost", "port": 5678 }, "pathMappings": [ { "localRoot": "/mnt/the_repo_folder", "remoteRoot": "/mnt/the_repo_folder" } ], "justMyCode": true } ] }
解决方向
1. 修正coverage禁用方式
launch.json的远程附加配置不支持env属性,要禁用coverage直接在pytest启动时加参数:
pytest --no-cov
或在VSCode的settings.json中配置pytest默认参数:
"python.testing.pytestArgs": ["--no-cov"]
2. 替换手动debugpy代码为VSCode原生pytest调试配置
不再手动插入debugpy.listen代码,直接使用VSCode生成的pytest调试配置:
- 打开「运行和调试」面板,点击「创建launch.json文件」,选择
Python→pytest - 生成配置示例(适配远程环境需确保路径映射正确):
{ "version": "0.2.0", "configurations": [ { "name": "pytest: Remote Test", "type": "python", "request": "launch", "module": "pytest", "args": ["your_test_path", "--no-cov"], "justMyCode": false, "pathMappings": [ { "localRoot": "/mnt/the_repo_folder", "remoteRoot": "/mnt/the_repo_folder" } ] } ] }
同时确保远程虚拟机内已安装debugpy:
pip install debugpy
3. 统一debugpy版本
检查本地和远程环境的debugpy版本是否一致,版本差异可能导致断点异常:
# 本地环境 pip show debugpy # 远程虚拟机内 pip show debugpy
统一版本示例:
pip install debugpy==1.6.6
4. 排查扩展冲突
暂时禁用Python扩展外的所有扩展(如代码覆盖率、格式化类扩展),验证是否为扩展干扰导致断点异常。
5. 调整路径映射与justMyCode设置
- 确认
pathMappings的本地/远程路径完全匹配,路径不匹配会导致调试器无法定位断点 - 将
justMyCode设为false,确保调试器能正确识别第三方库及测试框架内的断点映射
内容的提问来源于stack exchange,提问作者smallpants
相关产品推荐
相关产品推荐

