Cannot import name 'plot_image' from 'utils'

from utils import plot_image

Traceback (most recent call last):
File “ex1.py”, line 29, in
from utils import plot_image
ImportError: cannot import name ‘plot_image’ from ‘utils’ (/usr/local/lib/python3.7/dist-packages/utils/init.py)

1 Like

It seems that the utils package that you are calling is not the correct one. Try loading the utils.py from the examples folder, or that you can find here (i.e. copy the file to your working directory with your notebook).

1 Like

Thank you! It works correctly! :smiley:

Could you pleasae, give more detials?

The utils.py file containing the plot_image that is called in the code is an extra file that was added to the examples to help plot RGB images.

In order to use the function, and for the command: from utils import plot_image to work, you need to add the python file to the same directory as the script you are running (this is one solution). In my post above I provide the link where you can download the file.

Hello,

How can I load the utils.py package in the same folder as the notebook I’m working on? I work in jupyter notebook and the notebooks are saved in a local server. How can I copy the py file there?

Any help will be much appreciated.
Elena

Hi Elena,

If you don’t have the option to copy the file to the same directory as your Jupyter Notebook there is an easy alternative. Since the code is so short, you can just add it to a new cell in your Jupyter Notebook and call it from there. See the code below:

def plot_image(image, factor=1.0, clip_range=None, **kwargs):
    """
    Utility function for plotting RGB images.
    """
    fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(15, 15))
    if clip_range is not None:
        ax.imshow(np.clip(image * factor, *clip_range), **kwargs)
    else:
        ax.imshow(image * factor, **kwargs)
    ax.set_xticks([])
    ax.set_yticks([])