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

如何在VSCode中配置OUTPUT窗口以显示Python/C++程序输出?

How to Configure VSCode Tasks to Show Program Output in the OUTPUT Panel

Hey there! I totally get where you're coming from—switching from Sublime Text 3's straightforward Build Systems to VSCode can feel a bit disorienting at first. Let's fix that OUTPUT panel issue and get your Python/C++ program output showing up exactly where you want it, just like you're used to.

Quick Context: Debug vs. Tasks

First, a quick clarification: Pressing F5 triggers VSCode's debug mode, which by default routes execution to the Terminal panel. Tasks, though, are VSCode's equivalent of Sublime's Build Systems—they let you define custom build/run commands that can send output directly to the OUTPUT panel, matching your old workflow.

Step 1: Create Your tasks.json Configuration File

This file is VSCode's version of Sublime's .sublime-build file. Here's how to set it up:

  • Press Ctrl+Shift+B (or Cmd+Shift+B on Mac) to open the Tasks menu.
  • Select Create tasks.json file from template > Others to generate a basic template.
  • The file will open in the editor—now we'll tweak it for Python and C++.

Step 2: Configure a Python Task

Replace the default template with this code. It will run your current Python file and send all output straight to the OUTPUT panel:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Run Python (OUTPUT)",
            "type": "shell",
            "command": "python3", // Use "python" instead if that's your system's default command
            "args": [
                "${file}"
            ],
            "presentation": {
                "panel": "shared", // Reuse the same OUTPUT panel for every run
                "reveal": "always", // Automatically show the OUTPUT panel when executing
                "focus": false // Keep focus on your editor (optional tweak)
            },
            "group": {
                "kind": "build",
                "isDefault": true // Makes this task run immediately when pressing Ctrl+Shift+B
            },
            "problemMatcher": "$python" // Captures errors and sends them to the PROBLEMS panel
        }
    ]
}

Step 3: Configure a C++ Task

For C++, we'll set up a task that first compiles your code (using g++) then runs the executable, with all output going to OUTPUT. Make sure you have g++ installed on your system first:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Compile & Run C++ (OUTPUT)",
            "type": "shell",
            "command": "g++ -g ${file} -o ${fileDirname}/${fileBasenameNoExtension} && ${fileDirname}/${fileBasenameNoExtension}",
            "presentation": {
                "panel": "shared",
                "reveal": "always",
                "focus": false
            },
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "problemMatcher": "$gcc" // Sends compilation errors to the PROBLEMS panel
        }
    ]
}

Note: If you're on Windows, replace g++ with g++.exe and adjust the executable path syntax to use backslashes (e.g., ${fileDirname}\\${fileBasenameNoExtension}).

How to Run Your Task

  • Open your Python or C++ file in the editor.
  • Press Ctrl+Shift+B (or Cmd+Shift+B). If you set isDefault: true, it will run right away; otherwise, select your task from the pop-up menu.
  • The OUTPUT panel will now display your program's output exactly like Sublime's Build output did.

Key Configuration Tips

  • presentation settings: These control how the OUTPUT panel behaves. shared keeps you from getting a new panel every run, and reveal ensures you don't miss the output.
  • problemMatcher: This links task output to the PROBLEMS panel, just like Sublime's build system highlights errors for you.
  • Multiple tasks: If you need both Python and C++ tasks, add them to the same tasks.json file—you'll be able to pick which one to run when pressing Ctrl+Shift+B.

内容的提问来源于stack exchange,提问作者Daniel Correa

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.08 16:07:47