如何将VSCode调试器附加到Windows终端中运行的Python程序?
背景
我有一个用Python编写的个人项目游戏,使用blessed库在Windows命令提示符终端窗口绘制图形。
为辅助开发,我希望借助IDE的断点、单步执行等调试功能,但在IDE内置终端(已尝试Thonny和VSCode)运行脚本时,图形表现异常,因此尝试以下替代方案:
遇到的问题:
我尝试通过VSCode执行以下操作:
- 打开Windows命令提示符窗口,导航至项目目录,激活虚拟环境并执行
python game.py启动游戏 - 在VSCode中(已将Python解释器设置为运行游戏所用的虚拟环境),在game.py的游戏循环内设置断点
- 进入“运行和调试”面板,选择Python: Attach using Process Id
- 点击开始调试,打开进程选择下拉菜单
- 在下拉菜单中搜索“game”,选择标注为
python game.py的进程
预期结果:
- 调试器成功附加到命令提示符中运行游戏的Python进程,VSCode调试视图跳转到断点位置,支持变量查看、单步执行等操作
实际结果:
- 运行游戏的命令提示符窗口中打印
Unable to find python module. - VSCode等待约10秒后弹出错误窗口,提示
Timed out waiting for debug server to connect.
我的问题:
- 该方案是否可行?若可行,我操作中存在哪些问题,应如何修正?
- 是否有更适合该场景的替代调试方案?
补充信息
VSCode生成的launch.json调试配置如下:
{ // Use IntelliSense to learn about possible attributes. // Hover to view descriptions of existing attributes. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "Python: Attach using Process Id", "type": "python", "request": "attach", "processId": "${command:pickProcess}", "justMyCode": true } ] }
Python版本:3.11.3
虚拟环境已安装包信息(来自pipenv graph输出):
blessed==1.20.0 - jinxed [required: >=1.1.0, installed: 1.2.0] - ansicon [required: Any, installed: 1.89.0] - six [required: >=1.9.0, installed: 1.16.0] - wcwidth [required: >=0.1.4, installed: 0.2.8] opencv-python==4.8.1.78 - numpy [required: >=1.17.0, installed: 1.26.1] - numpy [required: >=1.17.3, installed: 1.26.1] - numpy [required: >=1.23.5, installed: 1.26.1] - numpy [required: >=1.21.2, installed: 1.26.1] - numpy [required: >=1.19.3, installed: 1.26.1]
内容的提问来源于stack exchange,提问作者Matt Smith
相关产品推荐
相关产品推荐

