rogo8888
rogo8888

Reputation: 951

VSCode: Why isn't debugger stopping at breakpoints?

I have just installed VS Code and the Python extension, and I have not been able to get the debugger to work. Every time I try to use the debugger, it just skips over any breakpoints that I have set and runs the program like normal.

I am using VS Code on a Windows 10 PC with Python 3.7.3 and the Python extension installed. I followed the instructions here (https://code.visualstudio.com/docs/python/python-tutorial) to make a test folder called 'hello' in C:\python_work\hello and create a program called 'hello.py' inside that folder. hello.py is shown below. I tried using the debugger both by pressing the green arrow and by pressing F5, but neither seemed to make the debugger work properly. My 'launch.json' file is also shown below.

hello.py:

msg = "Hello World!"
print(msg) # Breakpoint

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
        },
    ]
}

I expected the bottom bar to turn orange and the program to stop on the second line, allowing me to examine the local and global variables in the preview pane. Instead, the bottom bar stayed orange for 1/2 a second while the program ran as if I had pressed "Run Python File in Terminal," without stopping at the breakpoint. Please help!

Upvotes: 95

Views: 98396

Answers (15)

JustBeingHelpful
JustBeingHelpful

Reputation: 18980

To solve this, I had to do the following. I don't know which item(s) below solved the issue, but hopefully this will be helpful to someone. The Python debugger I was using was called Python File, as opposed to the other two optional Python debuggers, Python File with Arguments and Module.

1.) Deleted the file "pyvenv.cfg" in the working folder. It contained the contents. This got created automatically somehow from earlier debugger sessions.

home = C:\Users\me\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0
include-system-site-packages = true
version = 3.7.9

2.) The first time I created the virtual environment in GitBash with the Python File debugger not working was the following:

cd "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2544.0_x64__qbz5n2kfra8p0"
python3 -m venv --system-site-packages "C:\Python\source"

3.) The second time I created the virtual environment in GitBash, I added a new folder in my source directory (path of my Python file being debugged) called venv.

cd "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2544.0_x64__qbz5n2kfra8p0"
python -m venv --system-site-packages "C:\Python\source\venv"

Upvotes: 0

Steve Miller
Steve Miller

Reputation: 1

Weighing in late here but I'm not sure the version of Python is part of the issue. I think the real issue is with the 32 bit versus 64 bit versions of Python. I am on Python 3.10.11 and changed my interpreter from 64 bit to 32 bit and the debugger started hit the breakponts.

Upvotes: 0

mark salmoni
mark salmoni

Reputation: 11

This happened to me as I was walking through this VSCode tutorial.

My debugger did not work because I accidentally put the file in the .venv folder. When I moved it out of there it ran fine.

Upvotes: 1

user819640
user819640

Reputation: 250

I have been battling with this for the last hour, and it finally worked by renaming my python file from camelCase.py to snake_case.py

Upvotes: 0

Anna
Anna

Reputation: 47

I (re)downloaded VS Code again: https://code.visualstudio.com/ The breakpoint got to show up on hover and function with the Run and Debug for the python file I was working on.

Additionally, I had to solve a syntax error in my code before the debugger buttons (step over/in, etc) had to go through the code lines

Upvotes: 0

marcel h
marcel h

Reputation: 788

For everyone coming here looking for a solution related to breakpoints in python unittests:

  1. If not already done add a configuration file, i.e. launch.json, as described in the vscode python debugging tutorial.

  2. For me the following configuration settings worked, whereby setting cwd (because of src and test folders) and purpose (see also here) were essential.


"version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Test Cases",
            "type": "python",
            "request": "launch",
            "cwd": "${workspaceFolder}/afinad/src",
            "purpose": [
                "debug-test"
            ],
            "console": "integratedTerminal",
        }
    ]

Happy debugging!

Upvotes: 1

caucus
caucus

Reputation: 182

Another source of debugger not stopping at breakpoint: an homonym file, from another directory, is open in VS Code.

In fact, VS Code can set breakpoints on any Python file, even outside of your Python project's directory; if the path of the file does not match the one in launch.json, "program": [your-file-name-here] entry, then breakpoints are not hit.

For example: copies of Python source files are located under dist/lib, the output of setuptools (run: python setup.py bdist_wheel to generate them).

If dist/lib is not excluded through .gitignore, then its contents shows up in the search results sidebar and can be click open by mistake.

Caveat: the above does not hold if the launch.json configuration has "program": "${file}".

Upvotes: 0

rdtsc
rdtsc

Reputation: 1074

Another source of no-breakpoints-working is elevate:

try: # https://pypi.org/project/elevate/
    from elevate import elevate
except ModuleNotFoundError: # install it if not found
    subprocess.call([sys.executable, "-m", "pip", "install", 'elevate'])
finally:
    from elevate import elevate

# require elevated permissions right away...
elevate()

The elevate() command restarts the program as a new (child) process, so the entire file becomes un-breakpoint-able. From the author:

On Windows, the new process’s standard streams are not attached to the parent, which is an inherent limitation of UAC.

In windows, to debug python applications in VS Code which need to be run as administrator, a work-around is this:

  1. Close VS Code.
  2. Right-click VS Code icon, choose "Run as Administrator."

Linux users could likely do the same with sudo code. Now the process will be spawned initially as administrator, so the call to elevate() does nothing and the whole file is debuggable.

Upvotes: 1

MyNameIsTrez
MyNameIsTrez

Reputation: 662

My issue was that I was putting the breakpoint on the line of the function declaration itself, so the def foo(x): line, instead of putting the breakpoint on the first non-comment line inside of the function.

If the first line of your function is a comment, the debugger will assume you meant to place the breakpoint on the line of the function declaration and move your breakpoint back, so really make sure you aren't placing your breakpoint on a comment!

Upvotes: 2

QueensDog
QueensDog

Reputation: 31

Downgrading from Python 3.9 to 3.8 worked for me.

VC Code 1.56.2 ignored breakpoints running Python 3.9 64-bit on Windows 10 version 20H2. Installing Python 3.8.10 64-bit fixed the problem. I just needed to select the Python 3.8.10 interpreter within VS code and it now recognizes breakpoints. No changes to the configuration file were needed, for example, I did not need "justMyCode": false

I realize this is an old question, but my google search lead me to it. Many of the previous answers no longer apply. So I'm posting this fix for others who land here in 2021 trying to use 3.9.

Upvotes: 2

borcho
borcho

Reputation: 137

I recently found out ( 5th May 2020) that using Python 3.9 as interpreter the breakpoints were not hitting. Install 3.8 and it will start working again.

Upvotes: 0

Tomasz Bartkowiak
Tomasz Bartkowiak

Reputation: 14958

If you're using a pytest-cov module you might also wanna have a look at pytest configuration settings note:

Note If you have the pytest-cov coverage module installed, VS Code doesn't stop at breakpoints while debugging because pytest-cov is using the same technique to access the source code being run. To prevent this behavior, include --no-cov in pytestArgs when debugging tests, for example by adding "env": {"PYTEST_ADDOPTS": "--no-cov"} to your debug configuration.

See an example configuration file (launch.json) below:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Debug Tests",
            "type": "python",
            "request": "test",
            "console": "integratedTerminal",
            "justMyCode": false,
            "env": {"PYTEST_ADDOPTS": "--no-cov"}
        }
    ]
}

Upvotes: 64

Richard Newton
Richard Newton

Reputation: 31

I had the same problem having upgraded from Python version 3.7 to version 3.9. I later found out that the old version was 32 bit, and the new version I upgraded to was 64 bit, which apparently caused the issues. Uninstalling the 64 bit version and installing the 32 bit version of Python 3.9 solved my problem, and I was then able to use the Visual Studio Code breakpoint functionality again.

Upvotes: 1

spacetaxi2100
spacetaxi2100

Reputation: 41

I experienced the same behaviour and was able to solve it by installing the virtual environment of Python in the following way:

[MyProjectFolder] \ venv

by entering the command

python -m venv [MyProjectFolder]\venv

in the console.

VS Code seems to expect exactly that folder structure.

Before I had installed the venv-folder-structure directly in my projects-folder, i.e.

[MyProjectFolder] \ Scripts
[MyProjectFolder] \ Lib
[MyProjectFolder] \ Include
[MyProjectFolder] \ pyvenv.cfg

which did not work and caused exactly the described debug problems.

just as a reference: VS Code version 1.52.1 and Python 3.8.5

Upvotes: 4

Ashish
Ashish

Reputation: 1405

Setting "justMyCode": false makes it stop at breakpoint:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Debug Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "stopOnEntry": true,
            "justMyCode": false
        },
    ]
}

Upvotes: 124

Related Questions