1.使用说明
vscode python 远程调试pathMappings 配置
launch.json
"pathMappings": [{"localRoot": "本地代码目录","remoteRoot": "远程代码目录" # 注意不是运行目录, 是远程代码的目录}],
2.测试验证
测试目的:
远程代码目录, 与 运行工作目录 不一致时,
vsocde python 远程调试, 如何配置 remoteRoot, 才能进入断点调试模式
代码 2_pydebug_远程调试.py gitee在线代码
#!/usr/bin/env python3
# coding:utf-8
import time
import os
print(f"工作目录: {os.getcwd()}")
print(f"代码位置: {os.path.abspath(__file__)}")import debugpy
debugpy.listen(6688)
debugpy.wait_for_client()def cal_sum(num: int):i: int = 0sum: int = 0while i < num:sum += ii += 1time.sleep(1)return sumprint("计算1-10的和: ",cal_sum(10))
vscode配置 launch.json
{"version": "0.2.0","configurations": [{"name": "Python 调试程序: 远程附加","type": "debugpy","request": "attach","connect": {"host": "localhost","port": 6688},"pathMappings": [{"localRoot": "${fileDirname}","remoteRoot": "/workspace"}],"justMyCode": false}]
}
本地代码目录: /home/liuj/3_work/4_python-example/8_pip_modules/03_pydebug远程调试
远程代码目录: /workspace
运行程序目录: /home/liuj/
测试命令:
cd ~; /workspace/2_pydebug_远程调试.py
vscode python远程调试如图

3.总结
本次总结, 主要解决:
- 解决 ros2 python程序, 拷贝到设备上时, 代码目录与安装目录不一致, python远程调试配置问题
- 解决 调试pip install 库, 源码调试问题
- 解决 docker 映射目录, 与代码目录不一致时, vscode不能进入断点模式问题
