Nameerror: name ‘display’ is not defined

The nameerror: name ‘display’ is not defined is an error message you’ll encounter when working in Python.

Are you dealing with this error right now and having a hard time trying to figure out the solutions?

Keep on reading in order to understand this error and why it occurs in your Python script.

In this article, we’ll show you the solutions that will help you to troubleshoot this error.

What is display?

The display is not a built-in function in Python. Most likely, you’ll have to import a module (IPython).

What is “nameerror name ‘display’ is not defined”?

The nameerror: name ‘display’ is not defined occurs if you are trying to use the name “display” as a variable or function, but it is not defined or imported in your code.

In a nutshell, this error message can occur when you’re trying to use a function that is not defined yet or if you haven’t imported the necessary library or module that contains the display function.

It can also occur if you misspelled the name of the function or variable you are trying to use.

How to fix “nameerror: name ‘display’ is not defined”?

To fix the nameerror: name ‘display’ is not defined, ensure that “display” is defined or the required module or library that supports the display function has been imported before using it.

Solution 1: Import the “display” function

You can use the “display” function by importing from IPython.display import display. It will be available for use in your code.

For example:

from IPython.display import display

# Input the rest of your code here
display("Hi, Welcome to Itsourcecode!")

This solution assumes that you’re using the “display” function from the “IPython.display”
module.

Output:

Hi, Welcome to Itsourcecode!

To display an image, you can use the following code:

from IPython.display import display
✅ from PIL import Image

# Load the image
image_path = "sample.png"
image = Image.open(image_path)

# Display the image
display(image)

Solution 2: Define the “display” function

Define the “display” function before using it. By defining “display,” make sure that the function is available when it’s called.

For example:

def display(message):
    print(message)

# Rest of your code
display("Hi, Welcome to Itsourcecode!")

Output:

Hi, Welcome to Itsourcecode!

Solution 3:Use different function

If ever the “display” function or variable does not function well in your code, alternatively you can use a different name for the function or variable.
Here we used the function “show.”

def show(message):
    print(message)

# Rest of your code
show("Hi, Welcome to Itsourcecode!")

Output:

Hi, Welcome to Itsourcecode!

Solution 4: Use im.show() instead of display()

For example:

from PIL import Image
im=Image.open("sample.png")

# Lets display the image

im.show()

Here we used the Pillow library, a powerful image-processing library in Python.

The Image.open() function is used to open the image file, and im.show() displays the image using the default image viewer associated with your system.

You can install the Pillow library (PIL) using the following command:

pip install pillow.

Solution 5: Use default image viewer

For example:

✅ import subprocess
import platform

def display_image(image_path):
    system = platform.system()
    if system == 'Windows':
        subprocess.run(['start', image_path], shell=True)
    elif system == 'Darwin':
        subprocess.run(['open', image_path])
    elif system == 'Linux':
        subprocess.run(['xdg-open', image_path])
    else:
        print("Unsupported operating system.")

image_path = 'sample.png'
display_image(image_path)

The code will open the image using the default image viewer associated with the operating system.

Note: Ensure to replace ‘sample.png’ with the actual path of your image file.

Conclusion

In conclusion, the Python error message nameerror: name ‘display’ is not defined occurs if you are trying to use the name “display” as a variable or function, but it is not defined or imported in your code.

This article discusses what this error is all about and already provides solutions to help you fix this error.

You could also check out other “nameerror” articles that may help you in the future if you encounter them.

We are hoping that this article helps you fix the error. Thank you for reading itsourcecoders 😊

Frequently Asked Questions

Function-related NameError

NameError on a function usually means the function was not defined yet, the wrong name was used, or the function is in a different scope.

Common triggers

  • Function name typo. caclulate() instead of calculate().
  • Called before def. At module level.
  • Function in wrong module. Imported from a different module than expected.
  • Decorated function shadowed. Decorator returned wrong name.

Diagnostic pattern

# BAD — undefined function
def main():
    result = compute_total()   # NameError if compute_total not defined

main()

# GOOD — check spelling and scope
def compute_total():
    return 42

def main():
    result = compute_total()
    print(result)

main()

# For decorated functions, make sure functools.wraps is used
from functools import wraps

def my_decorator(fn):
    @wraps(fn)
    def wrapper(*args, **kwargs):
        return fn(*args, **kwargs)
    return wrapper

Best practices

  • Use editor autocomplete. Prevents most typo NameError.
  • Order definitions before calls at module level.
  • Use type hints. Editor flags undefined names immediately.
  • Use functools.wraps in decorators to preserve the original function name.
What is Python NameError and what causes it?

NameError is raised when Python encounters a name (variable, function, class) that hasn’t been defined in the current scope. Most common causes: typo in variable name, using a variable before assigning it, missing import, or referencing a variable that was defined inside a function but accessed outside it.

How do I fix ‘name X is not defined’?

Check three things: (1) Is the name a typo? Compare with the spelling where you defined it. (2) Did you import it? Add ‘from module import X’ or ‘import module’ at the top. (3) Is X defined in a different scope (inside a function, conditional branch, or with-block) that hasn’t executed yet at the point you’re using it? Move the definition before the use.

Why does my variable work in one cell but not another (Jupyter)?

Jupyter kernels keep state between cells. If you defined X in cell 5 and run cell 3 later, X exists. But after Kernel-Restart, only the cells you re-run define their variables. Always run cells top-to-bottom on a fresh kernel before submitting. Use ‘Restart and Run All’ to verify your notebook is reproducible.

What is the difference between NameError and AttributeError?

NameError: the name itself doesn’t exist anywhere in scope (typo, missing import, scope issue). AttributeError: the name exists and points to an object, but that object has no such attribute/method (typo on method name, wrong object type). NameError is about the variable; AttributeError is about what’s inside it.

Where can I find more NameError fixes?

Browse the NameError reference hub for 49+ specific fixes (NumPy, pandas, Jupyter, Python 2 to 3 migration). For Python scope rules see the Python Tutorial hub. For attribute-level errors see AttributeError.

Caren Bautista


Technical Writer at PIES IT Solution

Responsible for crafting clear, well-structured, and beginner-friendly content across the platform. Handles the writing, proofreading, and editorial review of tutorials, guides, and documentation to ensure every article is accurate, readable, and easy to follow.

Expertise: Technical Writing · Content Creation · Documentation · Editorial Writing · JavaScript · TypeScript · Python · Python Errors · HTTP Errors · MS Excel
 · View all posts by Caren Bautista →

Leave a Comment