0

When I execute a simple print('Hello world') by using "Run Python File" in VScode I get this output

barakiva@pop-os:~/Development/Educational/Languages/Python/lang$ /usr/bin/python3 
/home/barakiva/Development/Educational/Languages/Python/lang/vars.py Hello world

However, opening VScode's integrated console and typing python3 vars.py outputs normally with no clutter

barakiva@pop-os:~/Development/Educational/Languages/Python/lang$ python3 vars.py
Hello world
1
  • This is because vscode does not have its own terminal. It uses the built-in terminal of the system, so when you run Python file, it will first run the command to start the terminal and then run the file. Commented Jul 28, 2022 at 1:29

1 Answer 1

1

This happens because the code is run through the Terminal. In short, under the hood, VSCode opens a new terminal and executes the command to run your program using the full path. It's done this way to avoid possible conflicts, if you choose to e.g. use an external terminal with a custom configuration.

VSCode can also display the program output only, via the Debug Console (tab to the left from Terminal). To do this, you need to create a custom launch configuration.

In the Run and Debug tab click create a launch.json file (if you don't have one already) and select the Python File template. Then, in .vscode/launch.json change property "console": "integratedTerminal", to "console": "internalConsole" for the desired configuration. See the complete config below:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "internalConsole",
            "justMyCode": true
        }
    ]
}

With this in place, you can run your Python file like before, and see the program output in the Debug Console:

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

But isn't that feature for debugging? What about just executing a program instead of debugging it?
@BarAkiva The only difference is that the console won't open on its own (the terminal does). This can be fixed with "internalConsoleOptions": "openOnSessionStart"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.