Python Forum

Full Version: NameError: name “x” is not defined ... even though x is defined
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I keep getting this name error message when I just want to print the value of x.
The picture clearly shows that I am defining x [x = 5].
I have uninstalled and reinstalled both Python 3.10 and Visual Studio Code several times
but the error persists.
Even VS Code says it sees no problems in the workspace after typing in this simple program.
What should I do?


x = 5
print(x)
Error:
>>> print(x) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'x' is not defined
From your error, it looks like you're running the call to print in the REPL, given the >>> prompt. Did you also execute the assignment line at the REPL before that? Can you post the whole output from the REPL?
Thank you for your assistance. So the program runs inside the terminal window but how come an error appears when I try to run it in the code editor window? Is this a VS Code issue or a Python issue?
How do you "run it in the code editor window"? How are you doing that?
What I meant by “code editor window” was the window that appears above the REPL/terminal when you open a file in VS Code. There is where Intellisense operates.

If I try to run the program mentioned earlier in this thread from the code editor window,
then I get an undefined variable error message in the REPL/terminal.

If I run the program from within the REPL/terminal, I don’t get an error.

Also, when I write the code for the program mentioned earlier and press the Run Code triangle button in the upper right hand side,
I don’t get the value for x.
Instead, I get this:

Output:
[Running] python -u "c:\Users\Owner\Documents\New folder\stuff.py" [Done] exited with code=0 in 0.086 seconds
I know I can run code from within the REPL/terminal but I also want to run code from the code editor window.

I hope I’m making sense. I have no problem clarifying my issue.
There are lots of ways to run code in VSCode.

Are you using "Run Selection/Line in Python Terminal" from the context menu that pops up when you right click in the text editor view? I get this error of I run your program that way without making a selection.
x = 5
print(x)
Error:
File "<stdin>", line 1, in <module> NameError: name 'x' is not defined
Notice the error says the problem is in line 1, but obviously the error is in line 2. If you don't make a selection, this command uses the line containing the cursor. So it ran this program
print(x)
And this program has an error in line 1.

"Run Python File in Terminal" from the context menu works for me.

"Run XXX in Interactive Window" requires I install ipykernal to use. When I select this option it creates a jupyter like interface where I enter python code in cells.

I usually use the run arrow near the upper right corner ("Run Python File in Terminal") or the "Start Debugging" or "Run Without Debugging" options under the "Run" menu. All of these run your program without problems.

What extensions do you have installed in VSCode? I have Python and PyLance. I also installed Jupyter because I wanted to get a feel for how that works. I think installing the Jupyter extension is why I get the "Run XXX in Interactive Window" choices in the code context menu.

So how are you running your program when you have this problem?
(Oct-19-2021, 07:24 PM)deanhystad Wrote: [ -> ]I usually use the run arrow near the upper right corner ("Run Python File in Terminal") or the "Start Debugging" or "Run Without Debugging" options under the "Run" menu. All of these run your program without problems.

Thanks! These run options work for me too.
I get the name error when I press Shift-Enter to run the code but I think pressing those keys activates the REPL.
Or maybe the REPL is already activated and is just waiting for commands.

I just started using Python and VS Code for less than a week so I'm learning how to navigate both the language and the IDE.
I haven’t installed ipykernel yet.

(Oct-19-2021, 07:24 PM)deanhystad Wrote: [ -> ]I think installing the Jupyter extension is why I get the "Run XXX in Interactive Window" choices in the code context menu.

VS Code says I have Code Runner, Jupyter, Jupyter, Jupyter Notebook Renderers, Pylance, and Python but I don't see the "Run XXX in Interactive Window option.
What do you mean by "code context menu"?
Though you can probably change the shortcut, "Shift+Enter" does "Run Selection/Line in Interactive Window" in my VSCode. If you don't have a selection this will run the line containing the cursor. If that line is "print(x)" it is going to complain that x is not defined because as far as "Run Selection/Line in Interactive Window" is concerned, "print(x)" is the entire program.

By "code context menu" I mean the menu that pops up if you right click inside the code editor window.