Debugger Not Stopping at Breakpoints in VS Code for Python Scrapy - python

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.

Related

VScode,The Python path in your debug configuration is invalid

I keep getting this error. Other have faced this issue and have asked this question already and I have tried every single solution that was posted but I still get the error.
Things I have tried to solve this problem:
Uninstalled and reinstalled python and VScode.
ctrl+shift+p and added python interpreter to path.
manually entered path in launch.json. "python": "C:\Users\saura\AppData\Local\Programs\Python\Python310\python.exe"
Not sure what more can I do.
I messed around with conda, julia, anaconda without having much knowledge about it and I have a feeling that might be the issue. I did however uninstalled everything regarding conda, julia, anaconda.
If anyone has any idea what could I do, I would really appreciate it.
Here's the screenshot of json file. I do not see anything wrong with it yet.
Post the content of your launch.json file so that people can take a look at it.
You Configuration Should Look Like This:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Base",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": true,
"args": [],
"python": "path-to-python"
}
]
}
From menu bar of VS Code (Run), go to Open Configurations and paste above content after replacing path-to-python with yours.
Make sure you are using edited debug configuration when launching the debug for script.
Look in the lower right corner of your interface, where you are prompted to choose a python interpreter.
You should just click there or use Ctrl+Shift+P to open the command palette and choose Python:Select Interpreter, then choose the appropriate python interpreter.
I know you have tried many ways for this. But here's the problem, you haven't selected a python interpreter for vscode. If none of the methods work, try reinstalling the python extension.
Also, if the "python" configuration in your launch.json is the same as you showed in your question. then it is wrong, you should use / or \\. like this:
// Example 1
"python": "C:/Users/saura/AppData/Local/Programs/Python/Python310/python.exe"
// Example 2
"python": "C:\\Users\\saura\\AppData\\Local\Programs\\Python\\Python310\\python.exe"

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

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.

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

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.

Categories

Resources