8

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 %
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
harrycoin
  • 85
  • 1
  • 1
  • 4

3 Answers3

4

The simplest/quickest way I do this is by putting

print("\033c")

at the top of my file (or anywhere else where I want everything above that print("\033c") erased (so I can get rid of the textual noise my other print statements might be making).

In ASCII encoding the octal number 33 is the ESC (escape) character, which is a common prefix for terminal control sequences. In combination with c we have an old VT100 reference essentially asking the terminal to reset to default, thus erasing all that textual noise so we only see the output of our code.

Robert Houghton
  • 1,202
  • 16
  • 28
  • terminal reset sounds like overkill. why not just terminal clear? [`CSI 2 J` or `CSI 3 J`](https://en.wikipedia.org/wiki/ANSI_escape_code#CSI_(Control_Sequence_Introducer)_sequences)? – starball Feb 22 '23 at 07:48
2

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 requires the following form:

<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

{
    "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:

Debug Console screenshot

Unfortunately, if your Python code uses and requires manual input() from the user, using the Debug Console and the internalConsole solution would not work. It will show this error:

enter image description here

There is no direct solution to that, to having a "clean" output and allowing manual user input(). You might want to consider instead reading your program inputs from a file or passing them as command line arguments. VS Code supports passing in arguments ([args]) when running your script

<path/to/python/interpreter> <path/to/python/file> [args]

using the "args" option in the launch.json file. Here is a simple example with sys.argv:

python code

import sys

# This is always path to the script
arg_0 = sys.argv[0]

# Same sequence as "args" in launch.json
arg_1 = sys.argv[1]
arg_2 = sys.argv[2]

print(arg_1)
print(arg_2)

.vscode/launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "run-my-script",
            "type": "python",
            "request": "launch",
            "program": "${workspaceFolder}/path/to/your_script.py",
            "console": "internalConsole",
            "args": [
                "89",
                "abc"
            ]
        }
    ]
}

sample output

Debug Console with command line args

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
2

I ran into the same problem and I found an alternative solution. It is possible to clear the console after starting the program with a simple line. This will remove the initial path output in the terminal as well:

from os import system

system("clear")
print("Hello World")
  • I tried something like this but still had but still getting output under the say Hello World like dev@**********-Air pydev % – Michael1775 Feb 06 '22 at 01:55