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 😊

Leave a Comment