简体   繁体   English

Python NameError 异常,如何调试?

[英]Python NameError exception, how do I debug it?

I have a Python NameError exception, for example:我有一个 Python NameError异常,例如:

>>> def test():
...   var = 123
...
>>> test()
>>> print(var)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'var' is not defined

What are the steps should I take to debug it?我应该采取哪些步骤来调试它? How do I fix it?我如何解决它?

Step 1 - Find out where was the variable used:第 1 步 - 找出使用的变量在哪里:

Read the NameError exception:阅读NameError异常:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'var' is not defined

The last line contains the name of the variable, in this case var .最后一行包含变量的名称,在本例中为var Make sure the spelling is correct and you made no mistake.确保拼写正确并且您没有弄错。

Right above the NameError , you can see the file name and the line number.NameError正上方,您可以看到文件名和行号。 This is the location where you tried to use the variable.这是您尝试使用该变量的位置。

Step 2 - Figure out where was the variable defined:第 2 步 - 找出定义变量的位置:

Look for a var = or any function which contains var as the argument:查找var =或任何包含var作为参数的 function:

def func(var):
    ...

Where is the variable you're trying to use?您要使用的变量在哪里? Where does it exist?它存在于哪里?

Try finding that name in all of your Python files using ctrl+f in Windows or cmd+f in Mac.尝试使用 Windows 中的ctrl+f或 Mac 中的cmd+f在所有 Python 文件中查找该名称。

Step 3 - Try to understand the scoping rules:第 3 步 - 尝试了解范围规则:

  • If the variable was defined in a function:如果变量在 function 中定义:

    Am I in the same function?我在同一个 function 吗?

     def test(): var = 1 print(var) # This will not work. def test2(): print(var) # This will not work either.

    If you're not in the same function, try fixing by returning the variable:如果您不在同一个 function 中,请尝试通过返回变量来修复:

     def test(): var = 1 return var var = test() print(var) # This will work

    or by accepting the variable as an argument:或通过接受变量作为参数:

     var = test() def test2(x): # var is now named 'x' print(x) test2(var)

    You can also return multiple variables:您还可以返回多个变量:

     def test(): var1 = "hello" var2 = "world" return var1, var2 var1, var2 = test() print(var1, var2) # Prints hello world.

    If you are in the same function, did you use if statements and never entered them?如果您在同一个 function 中,您是否使用了if语句并且从未输入过它们?

     def test(): if False: # Never true var = 123 print(var) # This will not work
  • If the variable was defined in the class:如果变量在 class 中定义:

     class Test: var = 123 def func(self): print(var) # This will not work

    Make sure you use self to access it:确保您使用self访问它:

     class Test: var = 123 def func(self): print(self.var) # This will work

    pass it using a default parameter:使用默认参数传递它:

     class Test: var = 123 def func(self, param=var): print(param)

    or access it using the instance:或使用实例访问它:

     class Test: var = 123 def func(self, param=var): print(param) inst = Test() inst.var # This will work inst.func() # This will work
  • If the variable is the name of a function or class:如果变量是 function 或 class 的名称:

    Make sure you define before using it:确保在使用之前定义:

     func() def func(): # This will not work pass
  • If you can't find it or it's not in the same file:如果找不到或不在同一个文件中:

    Are you trying to import it and forgot?您是否尝试导入它并忘记了?

     import operator itemgetter # This will not work operator.itemgetter # This will work

    Are you sure you spelled it correctly?你确定你拼写正确吗?

For more information about scoping, see this answer .有关范围界定的更多信息,请参阅此答案

If everything failed and you still haven't found your answer, you're always welcome to open a question and ask, or write in the comments of your existing stackoverflow question.如果一切都失败了,而您仍然没有找到答案,那么随时欢迎您提出问题并提出问题,或者在您现有的 stackoverflow 问题的评论中写下。

Read the question checklist an provide a minimal example - make sure your example contains enough code to see the location where the variable is defined, and where it is used, but not too much code as it will be hard to understand.阅读问题清单并提供一个最小示例 - 确保您的示例包含足够的代码以查看变量的定义位置和使用位置,但代码不要太多,因为这将难以理解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM