VSCode Python write to Debug Console - python

I am brand new to VSCode with Python. Is it possible to write to the tab DEBUG CONSOLE (only) from Python code? I have Googled but found nothing on this.
The debugger does this internally, e.g. on a breakpoint set to Log Message. For the moment, if I use print() that does go to DEBUG CONSOLE, but it also goes to TERMINAL tab, which I'd rather it did not. The launch.json has "console": "integratedTerminal" if that is relevant.

You can set "console": "internalConsole" to have output go to the debug console.

Related

Debugger does not stop on breakpoints (python. VS code)

This is running Visual Studio Code (version 1.55.0 user setup) on Windows 10, with python 3.9.
Whenever I try to add breakpoints to my python files, it marks it in the GUI as a breakpoint (red circle), but when I try to debug it goes right over them as if I never included them at all.
As a test I created an empty folder with only a short python test file, and the debugger went right over the breakpoints.
For context here is the brief test file (I made every line a breakpoint):
print('test')
print('test2')
foo = 1+2
print(foo)
I tried initially to use the default debug configuration (by clicking run -> start debugging -> python file). When that didn't work I thought that maybe the default was having an issue, but even manually creating the json file did not fix it.
Json file for context:
{
// 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": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
}
]
}
This seems similar to a question posed here: Debugger Not Stopping at Breakpoints in VS Code for Python however, either my issue is caused by something else, or that information is outdated, because neither the solution of adding "justMyCode": false to the json file; nor just re-installing everything, fix anything.
A newbie reason why you are not stopping on breakpoints (especially if you are not used to VSCode):
If you run the code using the green arrow icon, the breakpoints will not be hit. You will be able to set breakpoints and see the red dot next to the line, but the debugger will not stop on any of them.
If you start execution with F5, then the debugger will stop on the breakpoint(s).
The green arrow icon is "Run Python in terminal" which ignores breakpoints, while F5 is "Continue" which invokes the debugger.
Are you using python 3.9.3? I had the same issue, but it is related to python version and was fixed on 3.9.4.
Source: https://github.com/microsoft/vscode-python/issues/15865

Python in VS Code: Not stopping at breakpoints

I am trying to debug a simple Python program that reads a CSV and writes a new one in VS Code. When I set a breakpoint, it gets skipped. I am able to use breakpoint() and get the basic Python debugger, but I'd prefer to be able to use the VS Code debugger. I found this SO post and this documentation, but neither resolved the issue. I am on Windows, Python version 3.9.1. I am not an experienced Python developer, so it's very possible I'm missing something obvious, but I have done my fair share of .NET development.
UPDATE 1: launch.json and code
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": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"stopOnEntry": true,
"justMyCode": false
}
]
}
For the code, I've set breakpoints all over the place trying to get it to work, but here is my main.py. I've tried a breakpoint on the line h.get_approvers():
import adp
import hierarchy
import expensify
import sys
h = hierarchy.Hierarchy()
h.get_approvers()
UPDATE 2: Terminal output when debugging
Loading personal and system profiles took 664ms.
PS C:\Users\...\OneDrive - ZoomInfo\Dev\Sandbox\PyTest> & C:/Python39/python.exe "c:/Users/.../OneDrive - ZoomInfo/Dev/Sandbox/PyTest/main.py"
Hello world
PS C:\Users\...\OneDrive - ZoomInfo\Dev\Sandbox\PyTest>
Update
The issue has been closed and I have received confirmation that it was related to the Python version 3.9.3 (64 bit).
https://github.com/microsoft/vscode-python/issues/15865
Original Post
This has been driving me nuts and needs further investigation, but what I noticed
Python 3.9.3 64 bit >> skips breakpoints
and
Python 3.9.2 64 bit >> works as expected
I have reproduced this multiple times just to ensure that I wasn't just solving a problem with a simple un-/reinstall.
I have raised an issue for this and I'll update this reply as soon as I find the proper root cause, but for now this solves the problem at my end...although not to my satisfaction.
https://github.com/microsoft/vscode-python/issues/15865
I noticed that in the terminal information you provided, the only paths used are "python.exe" and the file "main.py" path.
In VS Code, the debugging function of Python code is provided by Python extensions. Therefore, when debugging python scripts, it will use "python interpreter" (python.exe), "python extension" (.vscode\extensions\ms -python.python-2021.1.502429796\pythonFiles\lib\python\debugpy), and "python script" (.py file).
When I click the green run button in the upper right corner of VS Code, the path displayed on the VS Code terminal is only python.exe and the ".py" file: Run the code and it will not stay at the breakpoint.
When I click F5 or the "Start Debugging" button: Debug code it will stay at the breakpoint.
If it still doesn't work, please try to reinstall the Python extension and reload VS Code.
Reference: Python debugging in Visual Studio Code.

Property justMyCode is not allowed (trying to debug python code)

Asking this again as this wasn't really answered:
In python, VSCode debugger won't step into external code. Can't figure out how to edit "justMyCode" in launch.json
I'm just trying to debug some python in visual studio code.
Also I don't really know what I'm doing because I'm a java guy and not a python guy.
First, I tried using the python debugger, and put in some breakpoints. But then when I ran the program, it wouldn't stop at the breakpoints.
So then, I was googling and read that I need to change a setting in my launch.json configuration:
{
"name": "Python: Debug Current File",
"type": "python",
"request": "test",
"program": "${file}",
"console": "integratedTerminal",
"stopOnEntry": true,
"justMyCode": false
}
As you can see, I added a new configuarion with justMyCode set to false, and request set to test. It underlines both in green, saying Property is not allowed for justMyCode and Value is not accepted for request as test. I tried changing request to launch, but still the justMyCode error is coming.
What am I doing wrong?? Why is it so difficult to debug python in vs code when it is so much simpler in eclipse with java??
(python 3.7.1 extension installed (with debugging))
I want to get more information from you.
Here is the Python debug configuration I have taken, and it works well as it can let me debug the standard library. I just use the default Python extension debug configuration and adds '"justMyCode": false':
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": false
}
In a python file, I add this code:
import pandas as pd
and add a breakpoint on this line. When the debugger stop at this line, Click 'Step Into(F11)' to get into '__init__.py' file in the pandas package. And when I set '"justMyCode": true' configuration in launch.json file, I can't get into pandas package's file anymore.
So, normally, you just need to add '"justMyCode": false' and everything will works well.
First, only left this only debug configuration in launch.json file, exclude the debugger take the wrong debug configuration.
Second, If it still not work, you'd better reinstall the 'Python' extension. As debug ability was provided by 'Python' extension. And when you disable this extension, the debug configuration will not recognize 'justMyCode' and some other settings which were provided by 'Python' extension.
And you need to know, some code the debugger can not step into, such as 'os.getcwd()', 'sys.path', and so on.
And if the problem still exists, you'd better disable all the extensions, and just enable the python related extension. Even to create a new project to make a test.
Add "purpose": ["debug-in-terminal"] to launch.json. More details here.

Hiding the terminal tab in Visual studio code debugger

Trying to write something in python on Visual studio code and every time I run a debug it displays the terminal tab instead of the debug console tab, any way to stop this from happening?
When debugging python in Visual Studio Code, the place where the debug output is printed is defined by the console setting in your workspace's .vscode/launch.json file.
As can be seen in the documentation for python debugging in vscode, specifically in the console section, the default value for this is "integratedTerminal".
If you would like to override this, simply set the value to "none" in launch.json.
You can also go to your launch.json and set "internalConsoleOptions": "openOnSessionStart". This attribute should go in the configuration that you're currently using for debug, it's listed next to the green play button when debugging mode is selected. The attribute was specifically created for the purpose of displaying the debug console by default after debugging. My launch.json display:
That's also where you'll put the "console": "none" attribute if you go that route. You could do both for good measure: My launch.json display with both attributes:

Reading input during debugging in Python with VSCode

Here is the extension for python I used in the vs code: python extension.
When I use the debugging feature provided by the extension, it will hang in there and do nothing if it needs input from the command line.
Where can I input values to step over the input statement in the vs code?
The externalconsole directive is deprecated. Use console instead and indicate your preference for external this way:
"console": "externalTerminal"
The application output (and input) will go to a separate window so the VS Code debug console remains a pure python prompt where you can evaluate stuff during breakpoints.
The trick to getting this to work is on the extension's(Don Jayamanne's Python) wiki page. You have to include "externalConsole": true setting in your launch.json file's "name": "Python" section.
The extension's wiki confirms that this does not work by default:
This allows for capturing of input from the console/terminal window
applications, which isn't possible in the standard VSCode debugger.
Here are the steps to getting this to work:
From the Debug window (Ctrl+Shift+D), press the little gear icon to open (or to generate) a launch.json file. It gets placed into a .vscode directory in what ever folder you have selected as your "Open Folder" in VS Code.
You have to add pythonPath parameter to the first configuration block. This is needed to have the debugger work at all.
You also have to add and externalConsole parameter to the same block. This is what is needed to have the debugger accept input. When you debug, a separate window will open outside of VS Code but works well otherwise.
After you add both settings, the block should look something like this. I did not have to change anything else in the rest of the launch.json file.
{
"name": "Python",
"type": "python",
"request": "launch",
"stopOnEntry": true,
"program": "${file}",
"pythonPath": "C:/Users/igor/Documents/Tools/WinPython-32bit-3.4.3.7Slim/python-3.4.3/python.exe",
"externalConsole": true,
"debugOptions": [
"WaitOnAbnormalExit",
"WaitOnNormalExit",
"RedirectOutput"
]
},
The console option could have any of these values: internalConsole, integratedTerminal, externalTerminal.
Normally if you start the debugger and the program stops, it leaves the external terminal displaying a prompt Press Enter to continue . . . to have access to any output of the program. If you accidentally have a syntax error the external terminal just closes not leaving any message.
When using the integratedTerminal option, the terminal stays there and displays the error message.
While I do not know if this externalTerminal thing is a bug or not, the integratedTerminal option seems to work much better in this case.
VS Code has an option for you to Debug with Python console.
You just hit Ctrl + Shift + D and beside the blue play icon, click the down arrow, and choose Python Console App instead of just Python, like this:
Updated Information on Console Options
Most of the higher-rated, original answers are no longer valid, or unclear exactly what to set and how. See below for details how to set the "Console" option in launch.json, and what all the options are.
You can choose either an internal or external terminal setting and have keyboard input work during debug. One option for output, the Debug Console, does not currently (Fall 2019) allow keyboard input to your program, although you can always use the debug console to enter live debug and code commands.
The steps to set the available options are below.
Opening the Debug Configuration launch.json file
Click the debug icon (update early 2020 - now the "Run" icon): to open the debug sidebar (again, now called the "Run" sidebar, and the command menu name is also changed from debug to run).
At the top of the screen, ensure "Python: Current File" is selected. You may need to select it or create it (might need to create your first debug/run configuration):
Click the gear icon to the right of the configuration dropdown selected in prior step. This will bring up the launch.json for that configuration in the editor.
Update the "console": option to one of the settings described below
Valid "console" settings in launch.json
"console": "internalConsole"
this is the default setting
uses the internal debug console
as of 10/2019 does not allow keyboard input.
"console": "integratedTerminal"
this spawns a new Python Debug Console terminal window every time you debug (I wish it would reuse any existing one, but it doesn't - use the trash can symbol on the upper right of the terminal window to remove old, unused terminals)
The type of terminal created is based on the default terminal type you setup (i.e. command window, bash shell, etc.).
All standard output will be in this terminal and you can enter keyboard input if the program is waiting for it.
You can switch to the DEBUG CONSOLE tab if you want to run command during debugging.
"console": "externalTerminal"
this spawns a separate terminal outside of the VS Code process as the terminal for your code to run in on run or debug.
the external terminal will be the default type for your OS (command window for Windows 10).
this terminal is separate from VS Code and will normally add a Press any key to continue... prompt after your program terminates so that you can view/copy any output before it disappears.
All standard output will go to that terminal and keyboard input can be entered in it.
You can switch to the DEBUG CONSOLE in VS Code when the code is paused to enter debug commands.
In Visual Studio Code click the pick list to the right of the green arrow. Then select Python: Terminal (external). When you launch your script it will run in an external window and allow you to key in input.
Change the launch.json and put this into your java code
{
"type": "java",
"name": "Debug (Launch)",
"request": "launch",
"cwd": "${workspaceFolder}",
"console": "externalTerminal",
"stopOnEntry": false,
"mainClass": "",
"args": ""
}
simply:- step1. click on small gear-icon of debugger window. step2. make "true" to this ["externalConsole": false,] in launch.json file.
step3. and just restart your debugger.

Categories

Resources