python NameError:名称'< anything>'未定义(但它是!) [英] python NameError: name '<anything>' is not defined (but it is!)

查看:91
本文介绍了python NameError:名称'< anything>'未定义(但它是!)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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.

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

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.

这怎么会发生?

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?

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

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

谢谢.

如果您认为还有其他细节可以帮助解决此问题,我们将很乐意为您提供这些信息.

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

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

As requested, here is the function definition inside of 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() 

此功能在if __name__=='__main__':

我已经尝试过将InfraPy与脚本相关联.最令人困惑的情况是,当InfraPy与脚本位于同一文件夹中时,并且使用from InfraPy import listToText导入时,会出现此错误:NameError: name listToText is not defined.同样,其他函数也可以很好地导入,它们都在InfraPy中的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 folder as 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.

推荐答案

如果模块具有

This could happen if the module has __all__ defined

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

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

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

Is the NameError about listToText or is it something inside the function causing the exception?

这篇关于python NameError:名称'< anything>'未定义(但它是!)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆