Love our work? Support Us

How to Fix the Python NameError

Python NameError is an exception that is raised when a local or global name is not found. It occurs when a variable name is used that has not been defined. It usually happens when a variable name is mistyped or when a variable name is used that has not been assigned a value.


NameError indicates that a variable is not defined. Check if the variable is spelled correctly and is in the correct scope. If the variable is missing, define it. If the variable is in the wrong scope, move it to the correct scope. If the variable is an object, make sure the class is imported.


It is better to understand the roots of the Python NameError, as well as how to recognize and fix it.


In this post, we will identify the causes of the Python NameError and how to resolve them.

Python NameError Overview


As we have seen in the previous section above, a NameError occurs when a variable or function is used in Python but has not been defined. It usually results in a message that states the name used is not defined and includes a line number for reference. It is a common error when starting with Python as it is easy to forget to define variables or functions.


Let us look now what is NameError in Python?


What is NameError in Python?


NameError in Python is an error in Python that is raised when a name is not found in the local or global namespace. This error is usually caused by typos, incorrect scope, or referencing a variable before it is assigned. For example, we want to make a program that calculate the area of a rectangle. Here, we create a function called area_of_rectangle and then we can call it in its name as it is.


Example1:


Here, we get the NameError message because the function name is not defined like that. We can see this in the following image:

NameError

In the following section, we provide the causes of this Python NameError.


What Causes Python NameError


We have see above that Python NameError occurs when a variable is not found in the local or global scope; it is usually caused by misspelling, incorrect indentation, or a typo.


Now, let us look at some examples where the NameError can occur in a Python program.

  1. Naming Errors: This is when you try to call a variable or function that does not exist.
  2. Example2:


  3. Spelling Errors: This is when you accidentally misspell a variable or function.
  4. Example3:


  5. Uninitialized Variables: This is when you try to call a variable that has not been initialized.
  6. Example4:


  7. Unimported Modules: This is when you try to call a function from a module that has not been imported.
  8. Example5:


  9. Accessing Global Variables from Local Scope: This is when you try to access a global variable from within a local scope.
  10. Example6:



In order to better discuss this Python NameError, let's look at how to fix the NameError in Python in the following section.

How to Fix NameError in Python


As a reminder, NameError in Python occurs when a variable or function is used that has not been defined. To fix NameError, ensure the variable or function is correctly spelled, define the variable or function, or import the module that contains the variable or function.


Here are 5 examples on how to fix the NameError in Python:


  1. Check your spelling: One of the most common causes of a NameError is a typo in your code. Make sure that you have spelled your variables correctly and that you are using the same name when you declare and reference the variable.

  2. Check your indentation: Python relies heavily on indentation to determine the scope of variables and code blocks, so make sure that your code is properly indented.

  3. Check your imports: If you are using a library or module in your code, make sure that you have imported it correctly and that the module is accessible in the scope of your code.

  4. Check your variables: Make sure that the variables you are using in your code have been declared and initialized before you use them.

  5. Check your scope: Make sure that the scope of your variables is appropriate for the code you are writing. If you are trying to access a variable in a different scope, you will get a NameError.

  6. Let us use the practical example:


    Suppose that we want to make a program that calculate the magnitude of a vector in Mathematics. We need to import the Python module called numpy. This module is used for numerical calculations.


    Example7:


    Here we will get the NameError since we declared np but we used npn. We need to correct the mistake and use np.

Conclusion


As a summary, in order to fix a Python NameError, first check the code for any misspellings or typos. Check that all variables and functions are properly named, and that the correct syntax is being used. If the NameError is being caused by an imported module, check that the module has been imported properly. If the problem persists, it may be because the variable is not in the local or global scope. In this case, check the variable's scope or explicitly declare the variable.