Note. It is resolved. It turned out that I imported a previous version of the same module.
It is easy to find similar topics in StackOverflow where someone ran into a NameError. But most questions relate to specific modules, and the solution often updates the module.
In my case, I am trying to import a function from a module that I wrote myself. The module is called InfraPy, and it is definitely located on sys.path. One specific 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 fine. From InfraPy, I can easily import other functions. Including from InfraPy import * in my script does not return any errors until I try to use the listToText function.
How can this happen?
How to import one specific function returns a NameError, and the import of all other functions in one module works fine?
Using python 2.6 on MacOSX 10.6 also encountered the same error as when working with a script in Windows 7 using IronPython 2.6 for .NET 4.0
Thanks.
If there are other details that you think will be useful in resolving this issue, I would be happy to provide them.
As requested, here is the function definition inside 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 if __name__=='__main__':
I tried moving InfraPy relative to the script. The most difficult situation is that when InfraPy is in the same folder as the script and I import using from InfraPy import listToText , I get this error: NameError: name listToText is not defined . Again, other functions are imported fine, they are all defined outside if __name__=='__main__': in InfraPy.