NameError: function is not defined in Python

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

NameError: function is not defined in Python #

The Python "NameError: function is not defined" occurs when we try to call a function that is not declared or before it is declared. To solve the error, make sure you haven't misspelled the function's name and call it after it has been declared.

nameerror function is not defined

Here is an example of how the error occurs.

main.py
# ⛔️ NameError: name 'do_math' is not defined print(do_math(50, 50)) def do_math(a, b): return a + b

We tried to call the do_math function before declaring it, which caused the error.

At the time we called the function, it wasn't defined in our program.

To solve the error in this scenario, we have to move the call to the function after it has been declared.

main.py
def do_math(a, b): return a + b print(do_math(50, 50)) # 👉️ 100

The Python "NameError: function is not defined" occurs for multiple reasons:

  1. Calling a function that doesn't exist.
  2. Calling a function before it is declared.
  3. Misspelling the name of a function (names are case-sensitive).
  4. Using built-in modules without importing them first.
  5. Calling a scoped function from outside. For example, declaring a nested function and trying to call it from outside.

Make sure that you have defined the function before calling it and you aren't misspelling the function's name.

main.py
def do_math(a, b): return a + b # ⛔️ NameError: name 'do_Math' is not defined. Did you mean: 'do_math'? print(do_Math(50, 50)) # 👈️ function name misspelled

We misspelled the function's name (used a capital M) which caused the error.

Function names are case-sensitive, so make sure you aren't misspelling the function's name when calling it.

The "NameError: function is not defined" is also caused if you use a built-in module without importing it.

main.py
# ⛔️ NameError: name 'math' is not defined print(math.floor(17.7))
We use the math module without importing it first, so Python doesn't know what math refers to.

To solve the error, make sure to import any modules you are using.

main.py
import math print(math.floor(17.7)) # 👉️ 17

The error also occurs if you try to call a scoped function from outside.

main.py
def outer(): def inner(): return 'hello world' print('success') outer() # ⛔️ NameError: name 'inner' is not defined. Did you mean: 'iter'? inner()

The inner function is declared inside of the outer function, so we can't call it from the outer scope.

The best way to solve the error is to declare the function in the outer scope if we have to access it from outside.

main.py
def inner(): return 'hello world' def outer(): print(inner()) print('success') outer() inner()

An alternative in this scenario would be to return the result of calling the inner() function from the outer() function.

main.py
def outer(): def inner(): return 'hello world' result = inner() return result print(outer()) # 👉️ "hello world"

Conclusion #

The Python "NameError: function is not defined" occurs when we try to call a function that is not declared or before it is declared. To solve the error, make sure you haven't misspelled the function's name and call it after it has been declared.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.