Skip to content

VSCode Python配置

Python缩进问题

https://blog.csdn.net/Dr_Myst/article/details/104199272

利用Debugpy调试需要输入命令行参数的Python程序

编写 launch_train.sh 文件

# -e (errexit):错误立即退出; -x (xtrace):打印后面执行的命令及其参数;
set -ex
# 仅让设备编号为 0 的 GPU 对当前进程可见
CUDA_VISIBLE_DEVICES=0 \

python /home/username/data/.../train.py
--train_dataroot /home/username/data/...
--val_dataroot /home/username/data/...

在Vs Code内进行配置

  • 1.在自己需要使用的环境中安装debugpy:pip install debugpy -U

  • 2.Python程序启动文件 (如 train.py) import部分下面添加如下代码:python import debugpy try: # 5678 is the default attach port in the VS Code debug configurations. # Unless a host and port are specified, host defaults to 127.0.0.1 debugpy.listen(("localhost", 9501)) print("Waiting for debugger attach") debugpy.wait_for_client() except Exception as e: pass <img src="../../Assets/Images/vscode_debugpy_code.png" style="zoom:50%;" />

  • 3.在 launch.json 的 configuration 里面,加上如下配置:

python { "name": "sh_file_debug", "type": "debugpy", "request": "attach", "connect": { "host": "localhost", "port": 9501 } }, <img src="../../Assets/Images/vscode_debugpy_launchjson.png" style="zoom:50%;" />

  • 4.启动
    1. 打上断点。
    2. 终端运行:sh xxx.sh
    3. 等待终端出现:Waiting for debugger attach
    4. 在vscode的debug页面,选择 sh_file_debug进行debug。

<img src="../../Assets/Images/vscode_debugpy_debug.png" style="zoom: 80%;" />

  • 5.在终端 ctrl + c 中断程序,将第2步中添加的代码进行注释以关闭调试。