Spyder script scope, 5.3.3

Skip to first unread message

Ryan Enos

Feb 9, 2023, 8:27:53 PMFeb 9
to spyder
I just updated Spyder, and now I have the following issue.
If I enter in the console:
>>> a = 1
>>> print(a)
Then if I run the following single-line script:

I get the error:
NameError: name 'a' is not defined

Even though 'a' is clearly defined in the Variable Explorer.

According to chatGPT, Python has always worked this way, with a script apparently having it's own 'scope', without access to global variables.
I've used Python for years and never heard anything remotely like this. I access global variables from scripts all the time and never had an issue, in both ABAQUS and Spyder. Then suddenly I update Spyder and have this issue. Can someone please explain why it worked for me before and not now? And how I can fix this?

chatGPT says add the line 'global a', which is not acceptable solution for me even if it worked, but even that doesn't solve the error.


Carlos Córdoba

Feb 9, 2023, 8:48:40 PMFeb 9
to spyd...@googlegroups.com


This behavior was changed in Spyder 4 (if I’m not mistaken) to prevent people from inadvertently using variables defined in the console when rerunning a file.

For instance, they could have removed a variable in their code that it’s still present in the console from a previous run. So this would give the impression that everything is working fine, when in reality it’s not (i.e. the code will fail to run in another person’s computer). So, I’d say chatGPT is right about this one in the sense that the code you’re trying to run doesn’t stand on its own. Furthermore, it’s a bad practice to write code like that and should be avoided as much as possible.

But you can revert back to the old behavior by going to the menu

Run > Configuration per file

and selecting there the option

Run in console's namespace instead of an empty one.


El 30/01/23 a las 20:25, Ryan Enos escribió:

You received this message because you are subscribed to the Google Groups "spyder" group.
To unsubscribe from this group and stop receiving emails from it, send an email to spyderlib+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/spyderlib/4a403439-4d4f-4d08-8c3c-5ae247c749ffn%40googlegroups.com.

Reply all
Reply to author
0 new messages