Local script debugging for python in vscode - python

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.

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.

VS Code Python debugger won't connect to running process (timeout)

On my laptop I'm able to attach a VS Code debugger to a running python process, but on my desktop it always times out trying to connect.
Laptop: Debian Stable (11) , python 3.9
Desktop: Pop OS (Ubuntu 21.10), python 3.9.7
I'm signed in to a Github account so the VS Code settings and extensions are synced.
I'm using this simple test file:
import time
while True:
print("hello, world")
time.sleep(1)
Debugging works fine when I start the script from (the default) launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": true
}
]
}
But when I use the following (also default) launch.json to attach to my already running process, I always end op getting a timeout message (on the desktop, the same script does work on the laptop):
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Attach using Process Id",
"type": "python",
"request": "attach",
"processId": "${command:pickProcess}",
"justMyCode": true
}
]
}
Enabling logToFile and comparing the logs between my laptop and desktop doesn't show any difference, apart from the timeout happening, and the port used (port 33191 on my laptop, 35205 on my desktop, both using 127.0.0.1 as the host). Putting this port in my config doesn't work, but I'm also not completely sure where this port number comes from and how the debugger injects itself into the process.
It seems the debugger can find the process (I can select it from the dropdown) but somehow isn't able to actually connect to it. Does anyone know what might be wrong, or have any suggestions to point me in the right direction? I've tried a bunch of suggestions from other posts, but they don't see to work (mostly outdated or for running the current file instead of attaching to an existing process).
Digging a bit deeper I found there are multiple log files, one of which had an error about ptrace_scope (which I'd never heard of).
Simply said the value of the file at /proc/sys/kernel/yama/ptrace_scope determines what kind of processes debuggers can access. The different values are:
0: all processes can be debugged, as long as they have same uid.
1: only a parent process can be debugged.
2: Only admin can use ptrace
3: No processes may be traced with ptrace.
(list from here)
This file was set to 0 on my laptop (where attaching worked) but on my desktop it was set to 1, so I updated this value to 0 and now debugging works at expected.
echo 0|sudo tee /proc/sys/kernel/yama/ptrace_scope
edit:
I came across this issue on the VSCode repo:
https://github.com/microsoft/vscode/issues/146348

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.

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

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.

Categories

Resources