Warm tip: This article is reproduced from stackoverflow.com, please click
python visual-studio-code vscode-settings

How to hide file paths when running Python scripts in VS Code?

发布于 2020-06-28 09:38:39

Every time I run my code, the terminal at the bottom is displaying this long name (I think the file location) as well as whatever output it's supposed to display.

Is there a way to get that to go away?

This is what it looks like:

(screenshot of what's going on)

administrator@Machintosh-2 Exercise Files Python % /user/bin/python3...
Hello world!  
administrator@Machintosh-2 Exercise Files Python %
Questioner
harrycoin
Viewed
10
Gino Mempin 2020-04-13 08:38

AFAIK, there is no way to hide the paths, because the VS Code Integrated Terminal is basically using your OS/system's underlying terminal. And running Python scripts on a terminal usually requires:

<path/to/python/interpreter> <path/to/python/file>

If you want a "cleaner" console output, you could create a debug/launch configuration for Python, then set the console configuration to internalConsole:

.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": "run-my-script",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/path/to/your_script.py",
            "console": "internalConsole"
        },
        ...
    ]
}

That would display the output in the Debug Console tab rather than the Terminal tab.
There will be no more paths, just whatever your script prints out to the console.

sample output:

enter image description here