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

泄露秘密 提交于 2019-12-09 18:20:27

问题


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.

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?

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

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:

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__':

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.


回答1:


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

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




回答2:


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

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.

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

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



来源:https://stackoverflow.com/questions/4686525/python-nameerror-name-anything-is-not-defined-but-it-is

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