python NameError: name '<anything>' 未定义(但它是!)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4686525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 16:55:40  来源:igfitidea点击:

python NameError: name '<anything>' is not defined (but it is!)

pythonnameerror

提问by BenjaminGolder

Note: Solved. It turned out that I was importing a previous version of the same module.

注:已解决。原来我正在导入同一模块的先前版本。

It is easy to find similar topics on StackOverflow, where someone ran into a NameError. But most of the questions deal with specific modules and the solution is often to update the module.

在 StackOverflow 上很容易找到类似的主题,其中有人遇到了 NameError。但大多数问题都涉及特定模块,解决方案通常是更新模块。

In my case, I am trying to import a function from a module that I wrote myself. The module is named InfraPy, and it is definitely on sys.path. One particular function (called listToText) in InfraPy returns a NameError, but only when I try to import it into another script. Inside InfraPy, under if __name__=='__main__':, the listToText function works just fine. From InfraPy I can import other functions with no problems. Including from InfraPy import *in my script does not return any errors until I try to use the listToText function.

就我而言,我试图从我自己编写的模块中导入一个函数。该模块名为 InfraPy,它肯定在 sys.path 上。InfraPy 中的一个特定函数(称为 listToText)返回 NameError,但仅在我尝试将其导入另一个脚本时才返回。在 InfraPy 下if __name__=='__main__':,listToText 函数工作正常。从 InfraPy 我可以毫无问题地导入其他函数。from InfraPy import *在我尝试使用 listToText 函数之前,包括在我的脚本中不会返回任何错误。

How can this occur?
How can importing one particular function return a NameError, while importing all the other functions in the same module works fine?

这怎么会发生?
导入一个特定函数如何返回 NameError,同时导入同一模块中的所有其他函数工作正常?

Using python 2.6 on MacOSX 10.6, also encountered the same error running the script on Windows 7, using IronPython 2.6 for .NET 4.0

在 MacOSX 10.6 上使用 python 2.6,在 Windows 7 上运行脚本也遇到同样的错误,使用 IronPython 2.6 for .NET 4.0

Thanks.

谢谢。

If there are other details you think would be helpful in solving this, I'd be happy to provide them.

如果您认为还有其他细节有助于解决此问题,我很乐意提供。

As requested, here is the function definition inside of InfraPy:

根据要求,以下是 InfraPy 内部的函数定义:

def listToText(inputList, folder=None, outputName='list.txt'):
    '''
    Creates a text file from a list (with each list item on a separate line). May be placed in any given folder, but will otherwise be created in the working directory of the python interpreter.
    '''
    fname = outputName
    if folder != None:
        fname = folder+'/'+fname
    f = open(fname, 'w')
    for file in inputList:
        f.write(file+'\n')
    f.close() 

This function is defined above and outside of if __name__=='__main__':

这个函数定义在上面和外面 if __name__=='__main__':

I've tried moving InfraPy around in relation to the script. The most baffling situation is that when InfraPy is in the same folderas the script, and I import using from InfraPy import listToText, I receive this error: NameError: name listToText is not defined. Again, the other functions import fine, they are all defined outside of if __name__=='__main__':in InfraPy.

我试过将 InfraPy 与脚本相关联。最莫名其妙的情况是,当 InfraPy与脚本在同一个文件夹中,并且我使用 导入时from InfraPy import listToText,我收到这个错误:NameError: name listToText is not defined. 同样,其他函数导入正常,它们都是if __name__=='__main__':在 InfraPy之外定义的。

采纳答案by John La Rooy

This could happen if the module has __all__defined

如果模块已__all__定义,则可能会发生这种情况

Alternatively there could be another version of the module in your path that is getting imported instead of the one you are expecting

或者,您的路径中可能有另一个版本的模块正在导入,而不是您期望的版本

Is the NameErrorabout listToTextor is it something inside the function causing the exception?

NameError关于listToText还是函数内部导致异常的东西?

回答by Lennart Regebro

In addition the __all__variable gnibbler mentioned you could also have a problem with a InfraPy.pyc file lying around somewhere.

此外,__all__变量 gnibbler 提到您可能还会遇到位于某处的 InfraPy.pyc 文件的问题。

I'd recommend putting a import pdb;pdb.set_trace()first in the InfraPy.py file to make sure you are in the right file, and step through the definition of InfraPy.py to see what is happening. If you don't get a breakpoint, you are importing another file than you think.

我建议将import pdb;pdb.set_trace()第一个放在 InfraPy.py 文件中以确保您在正确的文件中,并逐步完成 InfraPy.py 的定义以查看发生了什么。如果您没有得到断点,则您导入的文件比您想象的要多。

You can also dir(InfraPy)after importing it, and check which file you are actually importing with InfraPy.__file__.

您也可以dir(InfraPy)在导入后检查您实际导入的文件是InfraPy.__file__.

Can't think of any more import debugging hints right now. ;-)

现在想不出更多的导入调试提示。;-)