如何在VSCode中配置OUTPUT窗口以显示Python/C++程序输出?
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(orCmd+Shift+Bon 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++withg++.exeand 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(orCmd+Shift+B). If you setisDefault: 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
presentationsettings: These control how the OUTPUT panel behaves.sharedkeeps you from getting a new panel every run, andrevealensures 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.jsonfile—you'll be able to pick which one to run when pressingCtrl+Shift+B.
内容的提问来源于stack exchange,提问作者Daniel Correa

