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
🌐
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 ...
🌐
Python Guides
pythonguides.com › nameerror-name-is-not-defined
NameError: Name is Not Defined in Python
September 3, 2025 - Python is case-sensitive. If you mistype a variable name, you’ll see this error. # Wrong City = "New York" print(city) # lowercase 'c' # Correct city = "New York" print(city) You can refer to the screenshot below to see the output. I’ve lost count of how many times I made this mistake when analyzing city-level datasets. Always double-check your spelling and capitalization. Sometimes, a variable is defined inside a function but is not accessible outside it.
Discussions

python - How to avoid "NameError: name is not defined" when using a class above its definition - Stack Overflow
I have a Python script and I am receiving the following error: Traceback (most recent call last): File "C:\Users\Tim\Desktop\pop-erp\test.py", line 1, in s = Someth... More on stackoverflow.com
🌐 stackoverflow.com
Name error : 'name' is not defined
The code below should perform the letter counter for a given language, but it gives the message: NameError: name ‘dutch’ is not defined. What I try to do is put the language in the function argument and then look for the file that has the name language.txt and run the program on it. More on discuss.python.org
🌐 discuss.python.org
1
0
April 1, 2022
NameError: name '' is not defined
Hello. I am learning Python, and this is my first app. I am getting this error: NameError: name ‘Verb’ is not defined. Massive thanks in advance for any help! """ Generate sentence type one """ def sentencetype1(Verb, Adjective, Noun): rand = random.randint(1,2) if rand == 1: text = "I ... More on discuss.python.org
🌐 discuss.python.org
0
0
August 14, 2024
python - NameError: name 'i' is not defined - Why does this happen? - Stack Overflow
I am making a rather simple wordle clone for my class. It functions exactly the same as wordle, just in a CLI. However, I am getting a weird error when testing. When executed, python returns Traceb... More on stackoverflow.com
🌐 stackoverflow.com
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
How to Solve 'NameError: name 'x' is not defined' in Python | Rollbar
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
How to Solve 'NameError: name 'x' is not defined' in Python | Rollbar
🌐
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

🌐
Rollbar
rollbar.com › home › how to solve an undefined variable nameerror in python
How to Solve 'NameError: name 'x' is not defined' in Python | Rollbar
March 17, 2025 - 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.
🌐
Python.org
discuss.python.org › python help
Name error : 'name' is not defined - Python Help - Discussions on Python.org
April 1, 2022 - The code below should perform the letter counter for a given language, but it gives the message: NameError: name ‘dutch’ is not defined. What I try to do is put the language in the function argument and then look for the file that has the name language.txt and run the program on it.
Find elsewhere
🌐
Python.org
discuss.python.org › python help
NameError: name '' is not defined - Python Help - Discussions on Python.org
August 14, 2024 - Hello. I am learning Python, and this is my first app. I am getting this error: NameError: name ‘Verb’ is not defined. Massive thanks in advance for any help! """ Generate sentence type one """ def sentencetype1(Verb, Adjective, Noun): rand = random.randint(1,2) if rand == 1: text = "I want to " if rand == 2: text = "I " verb_choices = random.sample(Verb, 12 + themevector1) text = text + random.choice(verb_choices) text + " the " if random...
🌐
TechGeekBuzz
techgeekbuzz.com › blog › python-nameerror-name-is-not-defined-solution
Python NameError: name is not defined Solution
The Python interpreter executes the code from top to bottom. Hence, you have to declare a variable before using it. If you use it without declaration, you get NameError. The most common type of NameError is NameError: name not defined.
🌐
freeCodeCamp
freecodecamp.org › news › nameerror-name-plot-cases-simple-is-not-defined-how-to-fix-this-python-error
NameError: Name plot_cases_simple is Not Defined – How to Fix this Python Error
June 27, 2022 - The resulting error was this: NameError: name 'math' is not defined. This happened because the interpreter did not recognize the math keyword. Along with other math methods in Python, we must first import the math module to use it.
🌐
Career Karma
careerkarma.com › blog › python › python nameerror name is not defined solution
Python nameerror name is not defined Solution | Career Karma
December 1, 2023 - This is because when you declare a variable or a function, Python stores the value with the exact name you have declared. If there is a typo anywhere that you try to reference that variable, an error will be returned. ... Traceback (most recent call last): File "main.py", line 3, in <module> print(boooks) NameError: name 'boooks' is not defined
🌐
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 - 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-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
A NameError "name 'X' is not defined" occurs when Python tries to access a variable that doesn't exist in the current scope. Fixing it when passing variables between functions requires choosing an appropriate data-flow pattern and applying correct ...
🌐
ONEXT DIGITAL
onextdigital.com › home › nameerror name is not defined python: what’s it and how to solve this
Nameerror name is not defined Python: What’s it and how to solve this
July 19, 2023 - Traceback (most recent call last): File "main.py", line 5, in <module> say_hello(message) NameError: name 'say_hello' is not defined ... We just need to move the function definition above the function call statement. So the Python interpreter can store the say_hello() function in heap memory and execute it when the function is called.
🌐
GeeksforGeeks
geeksforgeeks.org › python › nameerror-name-__file__-is-not-defined-in-python
Nameerror: Name '__File__' Is Not Defined" in Python - GeeksforGeeks
July 23, 2025 - In below example, the code print(__File__) will raise a NameError because the correct attribute name is __file__, not __File__. In Python, attribute names are case-sensitive, meaning that the capitalization of letters matters. ... Below, are the approaches to solve the "Nameerror: Name '__File__' Is Not Defined" In Python.
🌐
Code with Mosh
forum.codewithmosh.com › python
Keep getting Nameerror: name error is not defined - Python - Code with Mosh Forum
November 15, 2021 - Hi guys, I’m 40 minutes in to learning python with Mosh and I keep on getting this error when I run my script for the weight converter excercise: weight = int(input('Weight: ‘)) unit = input(’(K)g or (L)bs: ') # type: kgs if unit.upper() == ‘K’: converted = weight / 0.45 print("Weight in Lbs: " + str(converted)) else: converted = weight * 0.45 print('Weight in Kgs: ’ + str(converted)) Run: Weight: 80 (K)g or (L)bs: k Traceback (most recent call last): File “/Users/Oli/PycharmProje...
🌐
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' ...
🌐
Codementor
codementor.io › community › python errors: nameerror name is not defined and more
Python Errors: Nameerror name is not defined and more | Codementor
September 28, 2021 - The code above raised an error because line 3 is missing the closing parenthesis. You will encounter a nameerror ( name is not defined) when a variable is not defined in the local or global scope.
🌐
Bobby Hadz
bobbyhadz.com › blog › python-nameerror-name-is-not-defined
NameError: name 'X' is not defined in Python [Solved] | bobbyhadz
April 8, 2024 - The "NameError: name 'math' is not defined" means that we are trying to access a function or a property on the math module, but we haven't imported the module before accessing the property.
🌐
Reddit
reddit.com › r/learnpython › i know that 'nameerror: name '' is not defined' is a basic error but i can't think of a way to get trought.
r/learnpython on Reddit: I know that 'NameError: name '' is not defined' is a basic error but I can't think of a way to get trought.
March 19, 2023 -

Hi there!

I'm a programming beginner (So original! 😲) and a friend want me to code a cli based adventure game. So I opened python and realized I would probably need classes to not end up with a messy and unreadable tree-like code. Here is a program that contains an event class and a first event followed by a second event :

import climage
from colorama import just_fix_windows_console
import os
just_fix_windows_console()

terminalSize = os.get_terminal_size().columns
class Player:
	def __init__(self, name):
		self.name = name
	satiety = 10
	health = 10
	relationships = {}
	inventory = []

class Event:
	def __init__(self,
		pointsTo,
		text="",
		image=False,
		isImageUnicode=False,
		imageWidth=terminalSize-1,
		function=False,
		*args):

		self.text = text
		self.image = image
		self.isImageUnicode = isImageUnicode
		self.imageWidth = imageWidth
		self.pointsTo = pointsTo
		self.function = function
		self.args = args

	def play(self):
		if self.function != False:
			self.function(*self.args)
		if self.image != False:
			displaySize = self.imageWidth
			if displaySize > terminalSize-1:
				displaySize = terminalSize-1
			outputImage = climage.convert(self.image, is_unicode=self.isImageUnicode, width=displaySize)
			print(outputImage)
		print(self.text)
		wait = input("Press return to continue.")

class End:
	def play(self):
		print("Fini.")
		quit()


end = End()
event1 = Event(
	pointsTo=event2,
	text="This is event 1",
	image="images\\person.jpg",
	isImageUnicode=False
	)
event2 = Event(
	pointsTo=end,
	text="This is event 2",
	image="images\\city.jpg",
	isImageUnicode=False
	)
event1.play()

The problem is that when I define event1, event2 do not exist at all. To avoid this error, I would need to write my story backwards, it would be so painful and ugly. So do a clean solution even exists ?

Thanks for reading !