VSCode Python远程调试方法
网上各种教程都是抄来抄去,摸索两天终于找到成功路径。
1. 必要插件:remote-ssh,python extension for vscode
2. server端 pip安装debugpy
3. 代码最开始引入:
import debugpy
debugpy.listen(('0.0.0.0', 5678))
print("Waiting for client to attach...")
debugpy.wait_for_client()
4. 调试窗格添加配置Python: Remote Attach,也即在launch.json添加配置。
"configurations": [
{
"name": "Python: Remote Attach",
"type": "python",
"request": "attach",
"connect": {
"port": 5678
},
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "."
}
]
}
]
5. 保证下方terminal中当前目录是要执行的python脚本(xxx.py)所在目录;点击右上角的运行按钮(不是调试)或者在terminal中执行python xxx.py;当出现"Waiting for client to attach..."时,点击左侧调试窗格中的调试按钮(注意选择的配置是Python: Remote Attach),即可看到调试成功。