Define the class before you use it:

class Something:
    def out(self):
        print("it works")

s = Something()
s.out()

You need to pass self as the first argument to all instance methods.

Answer from Blender on Stack Overflow
🌐
Reddit
reddit.com › r/learnpython › nameerror: name 'name' is not defined
r/learnpython on Reddit: NameError: name 'name' is not defined
July 24, 2022 -

Hello,

I'm new to coding and don't know what I am doing wrong here. I am using VS Code, and below is what I've inputted. Only "john" and "programmer" are highlighted in red font, while the remaining code is either blue, green, or yellow. From my understanding, those phrases are causing the syntax error. Also, it works fine when I run the same code in IDLE Shell.

name = "john"

age = 21

weight = 160.1

occupation = "programmer"

>>>print(name, age, weight, occupation)

---->Traceback (most recent call last):

File "<stdin>", line 1, in <module>

NameError: name 'name' is not defined

🌐
Python
docs.python.org › 3 › builtins › exceptions.html
Built-in Exceptions — Python 3.14.7 documentation
Raised when a local or global name is not found. This applies only to unqualified names. The associated value is an error message that includes the name that could not be found.
People also ask

How to solve undefined variable in python
To solve the NameError: name 'x' is not defined error in Python, you need to make sure that the variable is properly defined and assigned a value before it is used. The variable should also be referenced correctly, with the correct case and spelling.
🌐
rollbar.com
rollbar.com › home › how to solve an undefined variable nameerror in python
NameError: name 'x' is not defined in Python
What causes undefined variable?
In Python, a variable is not created until a value is assigned to it. If an attempt is made to use a variable before it is defined, a NameError: name 'x' is not defined error is thrown. The error message typically includes the name of the variable that is causing the problem and the line of code where the error occurred.
🌐
rollbar.com
rollbar.com › home › how to solve an undefined variable nameerror in python
NameError: name 'x' is not defined in Python
🌐
W3Schools
w3schools.com › python › ref_exception_nameerror.asp
Python NameError Exception
Python Examples Python Compiler ... Plan Python Interview Q&A Python Training ... The NameError exception occurs if you use a variable that is not defined....
🌐
GeeksforGeeks
geeksforgeeks.org › python › handling-nameerror-exception-in-python
Handling NameError Exception in Python - GeeksforGeeks
July 1, 2026 - A NameError occurs when Python cannot find a variable, function, or identifier that is being referenced in the program. This usually happens when a name is misspelled, used before being defined or accessed outside its valid scope. ... ERROR!
🌐
Python.org
discuss.python.org › python help
"NameError: name is not defined"... but it is? - Python Help - Discussions on Python.org
October 27, 2021 - So, im making a tic-tac-toe game and im nearly finished i think. im trying to make it so the game can be played more than just once before it ends. So i put it into a def() function… but running it results in NameError: name 'turn' is not defined. Any ideas why and how to fix this? def game(): blanksheet = "|1|2|3|\n|4|5|6|\n|7|8|9|" sheet = blanksheet print(sheet) gone = [] turn = 0 def play(XO,player,listXO): global turn global sheet global listx global listo ...
Find elsewhere
🌐
Real Python
realpython.com › ref › builtin-exceptions › nameerror
NameError | Python’s Built-in Exceptions – Real Python
NameError is a built-in exception that occurs when you try to use a variable or function name that isn’t defined yet.
🌐
ScienceDirect
sciencedirect.com › science › article › abs › pii › S0950584924001976
Detecting and Explaining Python Name Errors - ScienceDirect
October 11, 2024 - To this end, DENE builds control-flow graphs for Python projects and leverages a scope-aware reaching definition analysis to locate identifiers that may cause name errors at runtime and report their locations. Experimental results on carefully crafted ground truth demonstrate that DENE is effective in detecting name errors in real-world Python projects.
🌐
YouTube
youtube.com › watch
Python NameError — What it is and how to fix it - YouTube
NameError: name '***' is not definedWhat is a NameError in Python? What can you do to fix it? When does it happen?All these questions are answered in this vi...
Published: August 19, 2024
🌐
Rollbar
rollbar.com › home › how to solve an undefined variable nameerror in python
NameError: name 'x' is not defined in Python
In Python, a NameError: name 'x' is not defined error is raised when the program attempts to access or use a variable that has not been defined or assigned a value.
Published: May 16, 2023
Top answer
1 of 2
2

The NameError is caused by undefined variables in cases where no values are found in the text file. Define them within the function before you try to assign values from the text file to them:

def resumes():
    f = open("resumes.txt",'r')
    for line in f:
        name = ""
        uni = ""
        sex = ""
        filename = ""
        for word in line.split():
            ...

You can also pre-define the variables in your class initialization by using keyword arguments if you like (this isn't the cause of the NameError though):

class resume:
    def __init__(self, name="", uni="", sex="", filename="")
        self.name = name
        self.uni = uni
        self.sex = sex
        self.filename = filename

Defining a list in python is done by typing mylist = [], not mylist[]. Also, at the moment, the list would be defined in the global namespace which is generally discouraged. Instead, you can make resumes return a list and assign this value to mylist:

def resumes():
    resume_list = []
    f = open("resumes.txt",'r')
    for line in f:
        for word in line.split():
            if word == ("John" or "Fred" or "Jim" or "Michael"):
                name = word
            elif word == ("Texas" or "Georgia" or "Florida" or "Montana"):
                uni = word
            elif word == "M":
                sex = word
            elif re.match(r'\w\.doc',word):
                filename = word
        r = resume(name,uni,sex,filename)
        resume_list.insert(r)
    return resume_list

Then you can do the following anywhere in your code:

mylist = resumes()

Remember to close files after opening them; in your case by calling f.close() after processing all the lines. Even better, have python manage it automatically by using the context manager with so you don't have to call f.close():

def resumes():
    with open("resumes.txt",'r') as f:
        for line in f:
            ...

Typically, you'd use append rather than insert when working with lists. insert takes two arguments (position/index, and the element to insert) so mylist.insert(r) should raise a TypeError: insert() takes exactly 2 arguments (1 given). Instead, do mylist.append(r) to insert r after the last element in the list.

As, johnrsharpe pointed out in the comments, your word comparisons probably aren't doing what you expect. See this example:

>>> word = "John"
>>> word == ("John" or "Fred" or "Jim" or "Michael")
True
>>> word = "Fred"
>>> word == ("John" or "Fred" or "Jim" or "Michael")
False
>>> 

Instead, use a tuple or a set and the keyword in to check if word equals any of the four names:

>>> word = "John"
>>> word in {"John", "Fred", "Jim", "Michael"}
True
>>> word = "Fred"
>>> word in {"John", "Fred", "Jim", "Michael"}
True
>>>
>>> type({"John", "Fred", "Jim", "Michael"})
<type 'set'>
>>> 

Finally, as Daniel pointed out, remember the colon, :, after function definitions such as def __init__(...)

2 of 2
1

Your code is throwing a NameError because at some point in the iteration of your file, some word variable doesn't fulfill any of the conditionals in this line of your function: if word == ("John" or "Fred" or "Jim" or "Michael"):, and name doesn't get defined.

The simplest way to workaround this error is to assign default values to your variables outside the scopes of your class and function (or within the scope of your function):

name = "name"
uni = "uni"
sex = "sex"
filename = "filename"

class resume:
# rest of your code

As an alternative, you could include conditional checks within your function for your variables; if the variable isn't yet defined, assign it a default value:

if "name" not in locals():
    name = "name"
r = resume(name,uni,sex,filename)

Finally, you'll want to append a colon to this line, from this:

def __init__(self, name, uni, sex, filename)

to this:

def __init__(self, name, uni, sex, filename):

change this line where you intialize mylist from this:

mylist[]

to this:

mylist = []

and change:

mylist.insert(r)

to:

mylist.append(r)
🌐
Quora
quora.com › What-does-name-is-not-defined-in-Python-3-mean
What does 'name ' ' is not defined' in Python 3 mean? - Quora
Answer (1 of 3): In Python [code ]NameError: name '???' is not defined[/code]: “Is raised when you tried to use a variable, method or function that is not initialized (at least not before). In other words, it is raised when a requested local or global name is not found.
🌐
GeeksforGeeks
geeksforgeeks.org › python › nameerror-name-plot_cases_simple-is-not-defined-in-python
Nameerror: Name Plot_Cases_Simple Is Not Defined in Python - GeeksforGeeks
July 23, 2025 - In this article, we will explore ... NameError is a common exception in Python, signaling that a variable or function with the specified name is not defined in the current scope....
🌐
Quora
quora.com › How-do-I-solve-the-problem-of-NameError-not-a-defined-name-in-Python-3-8
How to solve the problem of 'NameError' not a defined name in Python 3.8 - Quora
Answer (1 of 2): The Error means you are using a name that you have not already defined. Without seeing your code it is impossible to tell you exactly what the problem is, but a simple code example that generates the error: [code]my_name = 'Tony' print(my_Name) [/code]As you can see the error o...
🌐
CodeFatherTech
codefather.tech › home › blog › python error: name is not defined. let’s fix it
Python Error: Name Is Not Defined. Let's Fix It - Codefather
December 8, 2024 - In this article I will explain you what this error is and how you can quickly fix it. ... The Python NameError occurs when Python cannot recognise a name in your program. A name can be either related to a built-in function or to something you define in your program (e.g.
🌐
Quora
quora.com › How-do-I-resolve-a-name-error-in-Python
How to resolve a name error in Python - Quora
Answer (1 of 8): NameErrors are one of the most common types of Python errors. When you’re first getting started, these errors can seem intimidating. They’re not too complicated. A NameError means that you’ve tried to use a variable that does not yet exist.
🌐
Quora
quora.com › How-do-I-rectify-a-name-not-defined-error-in-Python
How to rectify a “name not defined” error in Python - Quora
Answer (1 of 2): Name not defined error comes, when you are using a variable that is not defined or declared previously. It is generally in the case when you do a typo. In my cases, I make the mistake of type arr —- as ar and then I get name not defined. Any variable before getting referenced, ...
🌐
Quora
quora.com › How-do-I-fix-the-name-error-not-defined-when-trying-to-pass-a-variable-between-functions-in-Python
How to fix the name error 'not defined' when trying to pass a variable between functions in Python - Quora
Answer (1 of 2): Such errors are usually fixed with reference to specific source code. However, answering in terms of the general case: the NameError exception usually arises in connection with confusions about scope, i.e. what names are accessible in any frame of execution? Remember that names ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › nameerror-name-__file__-is-not-defined-in-python
Nameerror: Name '__File__' Is Not Defined" in Python - GeeksforGeeks
July 23, 2025 - The "NameError: name 'file' is not defined" error occurs when attempting to access this attribute in a context where it is not recognized or not available. ... Traceback (most recent call last): File "<filename>", line <line_number>, in <module> ...
🌐
Finxter
blog.finxter.com › home › learn python blog › python’s nameerror: name ‘xxx’ is not defined — how to fix this stupid bug?
Python's NameError: name 'xxx' is not defined - How to Fix This Stupid Bug? - Be on the Right Side of Change
October 19, 2020 - The Python interpreter throws the NameError exception if it encounters an undefined variable or function name. To fix it, you must figure out why the variable is not defined—the most frequent bugs are (1) to use the variable or function name in the code before it was defined, or (2) to misspell ...