You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VS Code调试控制台如何保持开启?能否配置launch.json及其他方法?

如何让VS Code的Debug Console在Python代码运行后保持开启?

一、通过launch.json配置实现(调试场景)

如果是在调试模式下希望程序结束后Debug Console保持可访问状态,可以修改launch.json配置:

  • 打开调试面板(Ctrl+Shift+D),点击「创建launch.json文件」并选择Python环境。
  • 在生成的配置中添加或修改以下项:
    • 加入 "stopOnExit": true:调试器会在程序退出前暂停,需手动点击「继续」才会结束会话,Debug Console会一直保留内容。
    • 若偏好外部终端而非内置Debug Console,设置 "console": "externalTerminal":外部终端在程序结束后会自动提示「按任意键继续」,不会直接关闭。

示例launch.json片段:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: 当前文件",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "stopOnExit": true
        }
    ]
}

二、除time.sleep()外的其他方法

  • 添加输入等待逻辑:在代码末尾加入输入语句,让程序等待用户主动触发结束,比sleep更灵活:
    input("按回车键关闭控制台...")
    
  • 修改VS Code运行设置:如果使用「Run Without Debugging」(Ctrl+F5),可在设置中开启terminal.integrated.persistentSessionHistory,终端会话会保留历史内容,不会自动关闭窗口。
  • 用交互式模式运行:在VS Code集成终端中手动执行命令,程序结束后会进入Python交互式环境,控制台保持开启:
    python -i your_script.py
    

内容的提问来源于stack exchange,提问作者Cure-Dev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 18:45:38