OnextDigital logo 1
Success Stories
About Us
Pricing
Contact Us
Contact Us
footer logo

ONEXT DIGITAL helps your business grow through the utilization of technology and an innovative mindset. We have a history of going over and above because we not only complete projects on time – we find ways to support our customers to achieve their specific goals.

Services

  • Ecommerce Web Development
  • AI Development Services
  • Web App Development
  • Mobile App Development

About Us

  • About Us
  • Our Pricing
  • Insights
  • Success Stories

Contact

  • Chat With Us
  • info@onextdigital.com
  • linkedin
    google
    x twitter brands solid full

Copyright © 2026. ONEXT DIGITAL - By AHT TECH

blog detail placeholder

Looking for an Outsourcing Partner

Achieve 50- 70% cost savings on web/app development projects by hiring a skilled workforce at competitive projects s

Get a quote
Back
  • What is a NameError?
  • Common Error Examples
  • To sum up
  • FAQs

Need a Website or Mobile App From Experts?

We are ready to work and build on-demand solution for your business.

Back
  • What is a NameError?
  • Common Error Examples
  • To sum up
  • FAQs

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

By Editorial Team/January 31, 2023

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

FAQs

Yes. An offshore ClickFunnels agency can work effectively with Australian businesses when it has clear communication processes, suitable working hour overlap, reliable project management and relevant technical experience. The agency's location is less important than its ability to communicate clearly, meet deadlines and take responsibility for the work.

A ClickFunnels expert is usually suitable for straightforward work such as building a landing page, fixing an existing funnel or making specific changes. An agency is generally a better fit when the project involves funnel strategy, multiple systems, complex integrations, ongoing optimisation or a funnel that plays an important role in generating revenue.

Not necessarily. If the problem is conversion, landing pages or marketing automation, improve those areas first. Consider migration only when your business model has changed enough that your current ecommerce architecture no longer fits.

Yes, especially for a small product range where sales depend on paid traffic, offers, upsells or other funnel tactics. As catalog and operational complexity grow, Shopify usually becomes a better fit.

Need a Website or Mobile App From Experts?

We are ready to work and build on-demand solution for your business.