Python NameError: the name '<nothing>' is undefined (but this!)

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.

+6
source share
2 answers

This can happen if the __all__ module __all__ defined

Alternatively, there may be a different version of the module in your path that is imported instead of the one you expect

Is a NameError near listToText or is it something inside the function that throws the exception?

+3
source

In addition, the specified __all__ gnibbler variable mentioned that you might also encounter the InfraPy.pyc file, which is located somewhere.

I would recommend placing import pdb;pdb.set_trace() in the InfraPy.py file first to make sure you are in the right file, and run the InfraPy.py definition to see what happens. If you do not get a breakpoint, you are importing a different file than you think.

You can also dir(InfraPy) after import and check which file you import with InfraPy.__file__ .

I can no longer think of tips for debugging imports right now .; -)

+3
source

All Articles