Nameerror name is not defined Python: What’s it and how to solve this

NameeOne of the most frequent Python errors is NameError. And the “Nameerror name is not defined Python” occurs in Python when we try to access a Python variable before initializing or defining it. This Python tutorial will lead you through this problem and explain how to fix it. To help you understand how to fix this problem in your program, we’ll also offer a few examples to illustrate this Python error.

What is a NameError?

When attempting to access a variable or function that has not been defined in the program or imported libraries, Python raises the NameError.

For example:

message = "Hello World!"

print(Message)

Output:

Traceback (most recent call last):
File "main.py", line 3, in <module>
print(Message)
NameError: name 'Message' is not defined

Python is a case-sensitive programming language, which implies that it will regard two variables as separate ones if their cases differ. In the above program on line 1, we have defined a variable by the name “message” but in line 3 we are printing the variable “Message”, which is a completely different variable and not defined in the program.

Example Solution:

message = "Hello World!"

print(message)

Output:

Hello World!

Common Error Examples

Scenario 1: Missplet the variable or function name

This frequently happened when a programmer spelled the declared variable or function name incorrectly when attempting to access the variable or function. Python is a case-sensitive programming language, which means that even a single misspelled letter or case difference will result in an entirely different variable or function name.

Example

def say_hello(message):
print(message)

# the message to print
message = "Welcome to techgeekbuzz"

# error
sayhello(message)

Output

Traceback (most recent call last):
File "main.py", line 8, in <module>
sayhello(message)
NameError: name 'sayhello' is not defined

Solution

def say_hello(message):
print(message)

# the message to print
message = "W
elcome to techgeekbuzz"

# print message
say_hello(message)

Output

Welcome to techgeekbuzz

Scenario 2: Try to call a variable or function before the declaration

Since Python is an interpreted programming language that runs program code line by line, it is necessary to declare or define a function before invoking it or accessing a variable’s value. Before executing a function or accessing a variable, the function must be declared and the variable initialized, else we will get a NameError.

Example

# the message to print
message = "Welcome to techgeekbuzz"

# function call
say_hello(message)

# declare function after funtion call
def say_hello(message):
print(message)

Output

Traceback (most recent call last):
File "main.py", line 5, in <module>
say_hello(message)
NameError: name 'say_hello' is not defined

Solution

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.

# the message to print
message = "Welcome to techgeekbuzz"

# declare function before the funtion call
def say_hello(message):
print(message)

# function call
say_hello(message)

Output

Welcome to techgeekbuzz

Scenario 3: Forget to define a variable

We frequently remember a variable but fail to specify it in the program. And encounter the NameError when attempting to access it.

Example

name = "Rahul"

print("Name:",name, "nAge:", age )

Output

Traceback (most recent call last):
File main.py", line 3, in <module>
print("Name:",name, "nAge:", age )
NameError: name 'age' is not defined

We are attempting to print the age variable, which is undefined, in the program above. We are experiencing the NameError as a result. Before the print statement, the age variable and its value must also be defined in order to fix the mistake mentioned above.

name = "Rahul"
age = 30

print("Name:",name, "nAge:", age )

Output

Name: Rahul 
Age: 30

Scenario 4: Try to Access a Local Variable

There are two variable scopes in Python: local and global. Program space inside functions is referred to as the local scope, whereas program space outside functions is referred to as the global scope. The terms “global variables” and “local variables” refer to variables specified in the global and local scope, respectively.

A local variable can only be accessible inside its local scope; in contrast (inside the function), a global variable may be accessed through any scope. We see a NameError if we attempt to access a local scope variable outside of its functional scope.

Example

names = ["rahul", 'rajesh', 'aman', 'vikash']

def add_name(name):
# local variable total
total = len(names)

# add new name to names
names.append(name)

# add new name to names list
add_name("ravi")

# access all names
print(names)

# accessing local variable total
print(total)

Output

['rahul', 'rajesh', 'aman', 'vikash', 'ravi']
Traceback (most recent call last):
File "main.py", line 17, in <module>
print(total)
NameError: name 'total' is not defined

The NameError in the program shown above occurs at line 17 with the print(total) instruction.

Solution

names = ["rahul", 'rajesh', 'aman', 'vikash']

def add_name(name):
# add new name to names
names.append(name)

# local variable total
total = len(names)

# return the value of total
return total

# add new name to names list
total = add_name("ravi")

# access all names
print(names)

# print the value of total
print(total)

Output

['rahul', 'rajesh', 'aman', 'vikash', 'ravi']
5

To sum up

Nameerror name is not defined Python is a common exception variable in Python. To resolve this error we have to find the error line and make sure that the name we are using to access a variable or call a function must be defined in the Program.

If you still get this error in your Python program, please share your code in the comments; We will try to help you get rid of the error. By integrating reputable platforms like Magento eCommerce, BigCommerce, and Shopify, ONEXT DIGITAL provides top-notch eCommerce development services. With a trained team of specialists and engineers, we can overcome any e-commerce challenge, from deployment to migration.

>>read more

Shopify Compare At Price And How To Set A Compare At Price For A Product On Shopify