microsoft/debugpy

NameError: name 'x' not defined even though it should be

Emily-OBrien-NHS opened this issue · 25 comments

Type: Bug

I have been writing/debugging code line by line, using F9 or writing a line in the debug console, however recently it has stopped defining my variables. I set a breakpoint, debugging until that point works and variables show in the debug panel, and sometimes I can continue running lines for a time before it stops working. See example of screenshot of what I mean:
image

(the i variable was defined when running in debug mode until breakpoint, but the value doesn't update like it should have).

Extension version: 2024.8.0
VS Code version: 1.91.1 (user setup)
OS version: Windows_NT x64 10.0.19045
Python version (& distribution if applicable, e.g. Anaconda): 3.12.3
Type of virtual environment used (e.g. conda, venv, virtualenv, etc.): Venv

Not sure if it's something I've changed, but it used to work fine coding/debugging like this, but this issue started just under a week ago, and now I can't run any extra lines of code before getting the variable not defined issues. I think it's something to do with moving the breakpoint further into the code as I go? If I place the breakpoint a few lines in, then select the rest and hit f9, then I seem to be able to keep debugging line by line for a bit, wondering if there is some issues with setting the first breakpoint a long way into a piece of code?

This issue also happens to me in some of my code peices when I load a npz file, but when I copy this file loading things to a separate new script, I can set new variables in the debugging console normally, so I don't know what's the cause.

In some cases I'm able to update or set a new variable for only once from the debug console, but from the second attempt on, the modifications won't take effect.

this is similar to the issue here microsoft/vscode#216579.

my version info

Version: 1.91.1 (Universal)
Commit: f1e16e1e6214d7c44d078b1f0607b2388f29d729
Date: 2024-07-09T22:07:54.982Z
Electron: 29.4.0
ElectronBuildId: 9728852
Chromium: 122.0.6261.156
Node.js: 20.9.0
V8: 12.2.281.27-electron.0
OS: Darwin arm64 23.1.0

This really depends upon the context. If you step, the value can go out of scope. Do you have a code example that shows the problem?

For example in this scenario, if I create i in bar, and then run till the breakpoint in foo, i will no longer be defined. It's only in scope inside of bar

image

@rchiodo I understand that about variables in functions, but the code I'm running is mostly not using them at all, and the variables that I'm trying to define which are causing the error aren't the variables in the few user defined functions in my code. It's a really long script (3000 lines) that I'm trying to tidy up, and as I tidy bits and set a new breakpoint after what I've already tidied, it becomes worse. At ~600 lines I can no longer define anything without getting the error, making it impossible to tidy and debug any further.
In my example I'm trying to define a new variable within the debug console, away from the main code or functions etc, to test out new bits of code. Those new variables, which include a simple x=5 in the debug console as seen in my example, when called are saying that it's not defined, even though I defined it in the previous line.

A work around I've found is to place the breakpoint at an early point in the code, run to there, then select all the other lines I want to run up to the point I'm working at, press f9 and then continue debugging from there. So far this seems to be allowing me to define and use new variables, so from that I guess if you place the first breakpoint a long way into the code, it causes the issue?

Can you share an example piece of code? Breakpoints shouldn't have any bearing on whether or not variables are defined, only scopes should affect the result.

It could be your early breakpoint is at a more global scope so the variables survive into the later parts of the script.

Unfortunately I'm unable to share my code as it's work related. I am trying to create another example of the problem to share, but as has been commented, others have had the same issue on varying pieces of code. I'm using the libraries: pandas, sqlalchemy create_engine, textweap, matplotlib.pyplot, seaborn, numpy, scipy.stats, matplotlib.ticker, matplotlib.dates, scipy.stats.distributions, os

All the variables in my code up to hitting the break point when debugging are defined correctly as expected, that is not the issue. The issue is trying to change or define completely new variables within the debug console after the code has run to that breakpoint. It occurs when trying to define completely new variables that are unrelated to the code I'm running, as shown in my example with the x variable. There is no x in my code, regardless of the scope, this should be a completely new and unrelated variable defined in the debug console after having run the code to the breakpoint, but it throws the error.

hi @rchiodo, thanks for the reply. It's normal that if one steps to another function, the local variables defined in the previous function is lost. But at least in my case, I don't step into another function, I just pause the program at one line, define my new variable like a=1 (or try to update an existing variable). If I'm lucky I can define 1 new variable, but not a second one. During all these operations the program is paused at exactly the same line, no skipping, no stepping in or out.

I also tried to write a simple script to reproduce this problem, but havn't got a successful try. However, this problem happens to me EVERY SINGLE TIME I try to debug my own project where I need to load a bunch of saved numpy array and import some libraries. I'm not sure if importing and loading files will affect the debugger, but that's my guess. Do you have any idea that how we could provide more information on this issue? like enable verbose logging of the debugger extension to see what's happening under the hood?

Debugging in VS code supports this setting in your launch.json:

        {
            "name": "Launch: program",
            "type": "python",
            "request": "launch",
            "console": "integratedTerminal",
            "program": "${file}",
            "logToFile": true, <-- This setting here
        },

That logs everything the debugger does when stepping. It should log the request for the variable value too. The logs usually end up in the ms-python.debugpy extension folder. Like here for me:

C:\Users\rchiodo\.vscode\extensions\ms-python.debugpy-2024.11.2024080201-win32-x64

I don't believe there's much logging around eval failures though.

@Weijiang-Xiong your other issue looked like you had defined a lot of variables. Maybe there's some limit on the number of variables that the console allows. I'll try that to see if I can reproduce the problem.

No, that seems to have no effect. I also tried creating a bunch of numpy arrays to see if numpy had anything to do with it, no such luck.

In your screenshot in the dupe issue, it looks like the eval of line28b=1 never worked because it doesn't show up in the variable list. I would expect some sort of error in the debugger logs for that step. That might point out what the problem is.

I've added that line to my launch.json, mine is slightly different to yours, hopefully that shouldn't make a difference?

{
    // 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 Debugger: Current File",
            "type": "debugpy",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "cwd": "${fileDirname}",
            "logToFile": true
        }
    ]
}

It outputted several log files: debugger.vscode_..., debugpy.adapter-..., debugpy.launcher-..., debugpy.pydevd... and debugpy.server-...
I'm not sure which one would have the most useful logs, I can't see anything obviously linked to the error messages. I can copy and paste the last few entries in one of those which would match getting the error again:
image

Note: the first definition of x=5 happened at an earlier breakpoint, then I clicked to continue running and got the error at the next breakpoint/couldn't update the x value.

If you can zip up all of the logs I can take a look. There should be a message from the client to adapter asking to eval x=10 somewhere. Then a result should be returned. Then a request for eval of just x.

debug logs.zip
I can't share the full log files as they contain extracts of the data I'm using, which is sensitive. I've gone through and taken out the bits relevant to the errors, hopefully this is helpful? I re-ran it, so the commands are in a slightly different order, and all after one breakpoint at line 2213:
image

It looks like you removed all of the variable responses so it's hard to tell what's going on. You can e-mail them to me instead if you want. My e-mail is my user name at microsoft.com.

I logged my own version (where everything works) and it looks identical to the output you had, so the logs aren't going to be much help.

The code in question that actually updates locals after your expression eval is here:

for key, val in updated_globals.items():

You might try adding extra logging there to see if the updated_globals has your new x value. Like on line 92 you'd add something like:

info("Global val" + key + val)