'Error "variable is not defined", but the variable can be called in the console

I use python on Spyder. My script uses a variable called imgs. I have the following error : name 'imgs' is not defined. But when I try to call imgs in the console, it works, I don't have any error.

Then, I tried to write x=2 in the console of Spyder. Then, I executed a script on spyder (white juste 1 line : print(x)), and I still have the same error : name 'x' is not defined.

I never had this error before

enter image description here



Solution 1:[1]

That's because the console can remember recent variables defined in a script, but the script cannot once their gone.

Let me put it this way:

You run a script that imports the math module. Then you go to your script and delete the 'import math' statement.

You will still be able to use the math module without importing it again in the console, as long as you haven't closed the console after you ran the original working script.

Solution 2:[2]

You should try running in consoles's namespace Run Preferences

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ann Zen
Solution 2 Dror Laor