tomab tomab - 2 months ago 22
Python Question

VSCode doesn't show python script error output

Using Python3 with Visual Studio Code (Python extension installed) within Ubuntu 16.04.
I have some basic script written:

def mainMethod():

what()
#connectToDevice()

if __name__ == "__main__":
mainMethod()


When I debug this in Visual Studio Code by hitting F5 I can't see any output with the error in Debug Console:


Traceback (most recent call last):
File "main.py", line 9, in
mainMethod()
File "main.py", line 5, in mainMethod
what()

NameError: name 'what' is not defined


If I run
python3 main.py
in console the output appears.

How can I see those errors in VSCode and avoid switching back and forth between it and console?

Answer

I still can't see the output in Debug Console all the time but I can see it in the Integrated Terminal by setting this option in launch.json file of VSCode. The file looks like this:

{
"version": "0.2.0",
"configurations": [
    {
        "name": "Python virtual env",
        "type": "python",
        "request": "launch",
        "stopOnEntry": false,
        "console": "integratedTerminal",
        "program": "${workspaceRoot}/main.py",
        "debugOptions": [
            "WaitOnAbnormalExit",
            "WaitOnNormalExit",
            "RedirectOutput"
        ],
        "pythonPath": "${workspaceRoot}/env/bin/python3"
    }
]
}

The core line is "console": "integratedTerminal". Also please note that this configuration is using the interpreter from the Virtual Environment folder and the file which starts is always main.py instead of the default option which runs the file from the active editor.

Comments