问题
I am defining a variable test = 0
in Python 3.7.6 and then run the following code:
try:
test
except NameError as error:
print(error)
and I get the following output when running the code:
name 'test' is not defined
The variable test
however exists because I can call it in the console:
In [11]: test
Out[11]: 0
Any idea about what I am doing wrong there?
Best regards,
回答1:
A bit late maybe, but if it can help someone else, the issue is discussed and solved here. It is indeed an issue with Spyder 4. I could also not reproduce the behaviour with Spyder 3.3.5.
回答2:
I also can't reproduce your error, when test is defined.
test = 0
try:
test
except NameError as error:
print(error)
Now it works and there is no output. But if you try:
try:
test
except NameError as error:
print(error)
The output is now:
name 'a' is not defined
So probably your variable exists but you delete it later before you come to this code block. So the problem is definitely with the variable.
来源:https://stackoverflow.com/questions/60674468/spyder-4-try-except-nameerror-not-working