Spyder 4 try-except NameError not working

▼魔方 西西 提交于 2021-01-28 19:07:42

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!