Reputation: 2151
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?
Upvotes: 4
Views: 5777
Reputation: 101
I have a similar problem in my case was the Python Linter didn't show the errors.
My mistake was, that I open the VS Code when a VirtualEnv was active. Whit the command
code .
To fixed I close VS Code, delete de .vs folder, deactivate de VirtualEnv, then open VS Code and then open VirtualEnv.
And done!, problem solve. Other solution in my case would be install the linter (if I remember right, pylint is the default linter for vscode).
Upvotes: 0
Reputation: 2151
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.
EDIT: it seems that later versions solved this problem so above workaround doesn't apply anymore
Upvotes: 4
Reputation: 26
In the Feb 2018 update to VS Code this issue appears to be entirely resolved. So you don't need to make any changes to the default config, becuase the integrated debug terminal is finally printing errors when they occur.
Upvotes: 1
Reputation: 26
A slightly better way, for me, is to just add "console": "externalTerminal" rather than internalTerminal. That opens a new terminal when debugging, but it's a clearer one and it closes almost automatically.
Upvotes: 0