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

2024/4/15 0:55:24

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


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 = outputNameif folder != None:fname = folder+'/'+fnamef = 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.


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?

Related Q&A

Python File Creation Date Rename - Request for Critique

Scenario: When I photograph an object, I take multiple images, from several angles. Multiplied by the number of objects I "shoot", I can generate a large number of images. Problem: Camera gen…

Secure authentication system in python?

I am making a web application in python and I would like to have a secure login system.I have done login systems many times before by having the user login and then a random string is saved in a cookie…

Finding All Positions Of A Character In A String

Im trying to find all the index numbers of a character in a python string using a very basic skill set. For example if I have the string "Apples are totally awesome" and I want to find the pl…

DataError: (1406, Data too long for column name at row 1)

Ive read nearly all other posts with the same error and cant seem to find a proper solution. In my file I have this:class LetsSayCups(models.Model):name = models.CharField(max_length=65535)de…

Continued Fractions Python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 7…

Spark: equivelant of zipwithindex in dataframe

Assuming I am having the following dataframe:dummy_data = [(a,1),(b,25),(c,3),(d,8),(e,1)] df = sc.parallelize(dummy_data).toDF([letter,number])And i want to create the following dataframe: [(a,0),(b,2…

How to find list comprehension in python code

I want to find a list comprehension in python source code, for that I tried to use Pygments, but it didnt find the way to do that. To be more specific, I want to do a function that recognize all the po…

Save XLSX file to a specified location using OpenPyXL

Im having an issue saving my file to a certain location on my Raspberry PI (Raspbian) computer. Im wanting the XLSX file to be saved directly to my desktop rather than the folder holding the Python Sc…

Pandas read csv dateint columns to datetime

Im new to both StackOverflow and pandas. I am trying to read in a large CSV file with stock market bin data in the following format:date,time,open,high,low,close,volume,splits,earnings,dividends,sym 20…

Pydantic - Dynamically create a model with multiple base classes?

From the pydantic docs I understand this: import pydanticclass User(pydantic.BaseModel):id: intname: strclass Student(pydantic.BaseModel):semester: int# this works as expected class Student_User(User, …