How to make DEBUG CONSOLE as default tab in Visual Studio Code? - python

I'm using Visual Studio Code to learn Python. How can I make DEBUG CONSOLE as default tab in Visual Studio Code? Whenever I debug, this tool loads terminal which has lot of text which is confusing. I click on DEBUG CONSOLE tab to see my output. Wondering if there is any way to get rid of that terminal tab (just the tab, I understand we need terminal for code to work). I tried right clicking on TERMINAL tab and hiding it, but it hides only temporarily. Help appreciated.
Also, what does OUTPUT tab do?

You can press the hotkey to get the debug console:
Windows: Ctrl + Shift + D
macOS: shift + command + Y
Edit: In your project repo, find .vscode/launch.json file (create one if none exists) and add the following:
{
"version": "3.5",
"configurations": [
{
"type": "python",
"request": "launch",
"name": "Python: Current File",
"program": "${file}",
"console": "none"
}
]
}
Then relaunch VSCode for the change to take effect.

Related

Debugger Not Stopping at Breakpoints in VS Code for Python Scrapy

I know this question has been asked in the past but none of the previous answers have helped me. I'm writing a program using Scrapy to parse some web data and, for now, store it in JSON files. My debugging for a while was working fine, but I stopped working on the project, came back to it a few weeks later, and found that I couldn't get it to stop on any of my debug points.
Here's my 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": "Crawl with scrapy",
"type": "python",
"request": "launch",
"module": "scrapy",
"cwd": "${workspaceFolder}/DSRCrawler/DSRCrawler/spiders",
"args": [
"crawl",
"dsrSpider",
"-a",
"start=10/1/2022",
"-a",
"end=10/2/2022",
],
"console": "internalConsole"
}
]
}
From the Debug tab in VS Code, I try hitting the Play button that appears next to "RUN AND DEBUG" in the UI and tried the "Run" menu tab and selected "Start Debugging".
I've tried deleting the launch.json file and creating it again, I've tried reinstalling VS Code. I've tried the suggestion of adding the "justMyCode": false which doesn't seem to make a difference.
I have tried the solutions in the following links to no avail:
Debugger Not Stopping at Breakpoints in VS Code for Python
Why is VSCode not stopping at breakpoints for debugging?
One thing I finally found that works is using debugpy
import debugpy
...
debugpy.breakpoint()
But I'm confused why I should have to use this rather than the built in debugging and breakpoints. None of the other scrapy-specific solutions mention debugpy.

Local script debugging for python in vscode

I am trying to follow the instructions mentioned on the vscode page for debugging a local python script. According to the instructions, I have added the piece of code to my script. Next, I opened the integrated terminal and ran my script. I get the message waiting for debugger to attach.
Now I try to follow the step 6 of the instructions:
Switch to the Run view, select the appropriate configuration from the debugger dropdown list, and start the debugger.
So, I click on the run and debug button (one on the bottom left in the picture) and then click Python: Attach using process id (top of the picture).
However, the debugger does not get attached.
I am not sure where am I going wrong.
Please check the content of the file "launch.json" you use for configuration debugging:
{
// 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",
}
]
}
then click F5: The code stays at "debugpy.breakpoint()" as described in the documentation.

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.

run multiple python scripts at the same time

Is there a way to run multiple python scripts simultaneously in vsc. I mean while there is already a script running, I would like to run another script. When I try I get "code is already running".
In spyder-ide I simply open a new IPython console and run the new script in this newly opened console.
You can always open a terminal terminal window -- with either Python: Create Terminal or Open New Terminal -- and launch the script(s) manually in separate terminals.
If you need to coordinate execution and communicate between these programs, you'll need to use threading. If the scripts can run independently, you can run them manually at the same time from a terminal, or use a subprocess call from the first script:
subprocess.call(['python', 'secondscript.py', secondscript_arg1, secondscript_val1,...]).
You only need Ctrl + Shift + `
It will create a new terminal and you can run another script.
There is an extension called "Code Runner" extension developped by Jun Han, after install it, right click on the second script, select "Run Code".
Brief answer:
Create a debug configuration and run the script with Ctrl + F5. A button for this can be configured.
Elaborate answer:
There are multiple ways to run a Python file in VS Code. There is the triangular "Run" button (typically in the top right corner of the window) and there is the triangular "Run" button in the "Run and debug" view in the primary side panel. Use the latter one.
If there is no debug configuration yet, open the menu next to the run button and click on "add configuration ([your source folder])".
The launch.jason file should open.
Add a configuration like the one below (there can be multiple configurations, and you can adjust them to your liking):
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": true,
"cwd": "${fileDirname}",
"suppressMultipleSessionWarning": true,
}
]
Go to your source file and press Ctrl + F5. This will run your file with the configuration specified in the debug view, but the file will not be debugged.
You can run the same file or different files simultaneously that way. If the option suppressMultipleSessionWarning is not set or set to false, you will see a warning.
Because the "default" run button in the top right corner is typically not doing what I want, I disabled it (right click -> disable "run and debug").
Adding a "proper" run button to the task bar
If you do not like to use your keyboard, you can create a button for running. You may use the extension "Task Buttons".
Install the extension.
Add a task to your tasks.json like this:
"tasks": [
{
"label": "Run",
"type": "shell",
"command": "${command:workbench.action.debug.run}",
}
]
Then add a button that executes this task (in settings.json):
"VsCodeTaskButtons.tasks": [
{
"label": "Run",
"task": "Run",
"tooltip": "Run Python file",
}
]
Disclaimer: I did not have time to double check my answer in a fresh installation of VSCode without extensions. If the answer does not work for you, please write a comment, and I will do my best to check which extensions may be required.
open a new angle of the visual studio then open the other file in this new one, so you can run it
You could install PyCharm which has a plugin called 'Multirun'.This allows you to run several python files in parallel. I had the same issue as you and fixed it this way.
Use Sublime Text 3 and run your script by Ctrl + B shortcut

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