Python in VS Code: Not stopping at breakpoints - python

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.

Related

How to extend Python search path in Visual Studio Code

From what I found in the community, there are two options to extend the Python search path with further libraries.
.env
e.g. PYTHONPATH="C:\path\to\a;C:\path\to\b"
launch.json
e.g.
{
// 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",
"justMyCode": true,
"env": {
"PYTHONPATH": "${workspaceFolder}/lib"
}
}
]
}
After searching again for this topic on multiple days, I did not yet find any clear solution for that.
Some things work as expected, others don't. To be clear, it might be just a wrong understanding of the
concepts. Since I found multipe threads on the topic, but almost just the same answers, which did not
yet work out for me. There are multiple threads aroung that stuff, so I assume some general interest for a clarification.
Currently I tested exlusively each option. Using both things in parallel is somehow not comfortable,
also since the capabilities are different. .env does not support pre-defined variables,
lauch.json, does. So I tested .env without settings in launch.json and no .env file + settings in launch.json.
I use VS Code 1.75.1, but it was the case also for multiple previous version.
Anaconda Python3 with different virtual environments.
What I see is the following, when using the options in VS Code:
yes --> extending the PYHTONPATH worked as expected
no --> extending the PYHTONPATH dir NOT worked as expected
Run option
.env setting
launch.json setting
Run Python file in terminal
no
no
Run in interactive Window
yes
no
Debug via F5
yes
yes
"Debug Python File" (*)
yes
no
(*) Debug via "Debug Python File" in Drop-Down of Run Symbol above right of the editor window
I assumed both debug options should be the same, but I have sometimes situations, where starting Debug via F5 works as expected, but using the Debug Symbol reports an error.
Not clear, why both debug options behave differently.
No idea, why running Python terminal, does ignore path settings completely.
In general more detailed information about the ideas behind both optios, would be very helpful. Why there is a difference for just running code in the terminal and debugging. The concept of launch configurations I know from other IDEs and it makes a lot of sense to me. Is there a way define such conigurations hiarchically, so only changes have to be added for lower levels and the rest can be kept common.
Setting all environment stuff on OS/command shell level, is possible via system/user variables. But I thought, that VS Code is doing that specifically for each workspace/project.
What am I doing wrong? What do have I missed during setup of VS Code? Or where do I not yet understand the concecept/workflow correctly.
I expected to control the library path settings for each workspace seperately. Potenitally in just one place (one configuration setting) in the workspace area (.env or launch.jason, or what ever other way)
Any help/comment is appreciated, thanks a lot,
Mike
For the questions raised in your article, if you're only interested in debugging a Python script, the simplest way is to select the down-arrow next to the run button on the editor and select Debug Python File.
If you're looking to debug a web application using Flask, Django or FastAPI, the Python extension provides dynamically created debug configurations based on your project structure under the Show all automatic debug configurations option, through the Run and Debug view(f5).
You can read document basic-debugging for more detials.
I expected to control the library path settings for each workspace
seperately.
I suggest you using virtual environment. This makes it easier for you to manage your library.
In addition, if you want to import the package in the specified location, you can use env in the launch.json file:
"env": {
"PYTHONPATH": "path/to/your/package"
}

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"

Using vscode debugger on python module with active virtualenv, "the python path in your configuration file is invalid" in launch.json

I want to debug a python module in vscode to save time and help me figure out what's going on with the code. But I'm having two problems (I'll focus more on the 1st issue for this post) that seem like they're related to me not using launch.json correctly and I would like to know what's going wrong. I'm working on a Mac.
My module is called __main__. The full absolute path to it would be ~/Code/APPRES-483/cqs/update-query-definition/__main__.py (__main__.py is a script, but it seems the module is called __main__ as well).
There's a virtualenv at ~/Code/APPRES-483/venv-cqs/ that I'm using. I opened up a VSCode window inside of ~/Code/APPRES-483/cqs/update-query-definition/.
Right now my launch.json file looks like this.
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Module",
// "python":"~/Code/APPRES-483/venv-cqs/bin/python",
"type": "python",
"request": "launch",
"module": "__main__",
"pythonArgs":[
"-v",
"--file-to-write", "~/Code/APPRES-483/LOCAL-query-definition.yml"
],
},
]
}
The first problem is that I can't actually set the python interpreter path in launch.json. I have my virtualenv active inside of VSCode (i.e. I see the prefix for venv-cqs that I intend to see).
If I open up a terminal and put ~/Code/APPRES-483/venv-cqs/bin/python, that correctly opens up the python terminal/executable inside of venv-cqs/bin. If I add a line like "python":"~/Code/APPRES-483/venv-cqs/bin/python", I get the message saying "the python path in your debug configuration is invalid."
I tried to follow https://stackoverflow.com/a/66885651/6432861 instructions to use the Python: Select Interpreter command. I'm getting some weird behavior.
If I try to browse finder/files to find the python executable in venv-cqs/bin/, once I double click on the file it doesn't actually get made into the python interpreter for vs code. If I hover my mouse in the bottom left, I'm still seeing the address of my default python installation in /usr/local/bin or wherever it is.
But if I copy and paste ~/Code/APPRES-483/venv-cqs/bin/python rather than trying to browse for files, that successfully changes the python interpreter and I can see that at the bottom left of my screen.
The only way that the code avoids these errors is if I don't have a python field in the configuration for launch.json. I know that's not how it's supposed to be so... I want to know what's going wrong.
The second problem I'm having is with pythonArgs. But I'll try to make another post about it since this is already long enough.
First, you may set
"python.defaultInterpreterPath": "~/Code/APPRES-483/venv-cqs/bin/python",
in User Settings.json to specify python interpreter. If it doesn't work and VS Code still shows default python installation as python interpreter in bottom-left corner, deleting the user data folders $HOME/Library/Application Support/Code and .~/.vscode can reset VS Code.
Second, the setting python defaults to the interpreter selected for your workspace, so once you have selected the venv-cqs as python interpreter, debug would use it by default and if you insist that, set
"python": "${command:python.interpreterPath}"

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