How to Fix ‘ModuleNotFoundError: No Module Named keras.utils.vis_utils’ in Python

When working with Keras in Python, especially for deep learning projects, you might encounter the error: ModuleNotFoundError: No Module Named ‘keras.utils.vis_utils’. This frustrating error typically appears when trying to visualize neural network models.

In this tutorial, I will walk you through several proven methods to resolve this error based on my decade-plus experience as a Python developer.

I have faced this issue multiple times while building models for clients in the healthcare sector and finance industry in the USA, and I know exactly how to fix it.

The Error

Before getting into the solutions, let’s understand what causes this error:

The error occurs because the vis_utils module has been relocated or renamed in newer versions of Keras and TensorFlow.

In older versions, the visualization utilities were located in keras.utils.vis_utils but in newer versions, they’re often found in tensorflow.keras.utils.vis_utils or have been renamed entirely.

Method 1: Update Your Import Statement

The simplest solution is to update your import statement to match the current structure of the Keras library you’re using.

# Instead of this:
from keras.utils.vis_utils import plot_model

# Try this:
from tensorflow.keras.utils import plot_model

I’ve found this works for most TensorFlow 2.x installations, which is now the standard framework that includes Keras.

Read Module ‘tensorflow’ has no attribute ‘get_variable’

Method 2: Install the Correct Dependencies

Sometimes the error occurs because you’re missing the required dependencies. Make sure you have all the necessary packages installed:

pip install tensorflow
pip install pydot
pip install graphviz

I executed the above example code and added the screenshot below.

modulenotfounderror no module named 'keras.utils.vis_utils'
no module named 'keras.utils.vis_utils'

For a real-world US-based project I worked on involving medical image classification, I had to specifically install the Graphviz system package before the Python packages would work correctly:

# On Ubuntu/Debian
sudo apt-get install graphviz

# On macOS
brew install graphviz

# On Windows
# Download and install from https://graphviz.org/download/

After installing these dependencies, the visualization functions worked perfectly for our healthcare compliance models.

Check out AttributeError: Module ‘tensorflow’ has no attribute ‘variable_scope’

Method 3: Use Alternative Visualization Tools

If you’re still facing issues, you can use alternative visualization tools:

# Using TensorBoard instead
import tensorflow as tf
from tensorflow import keras

model = keras.Sequential([
    keras.layers.Dense(64, activation='relu', input_shape=(10,)),
    keras.layers.Dense(32, activation='relu'),
    keras.layers.Dense(1, activation='sigmoid')
])

# Log model architecture
log_dir = "logs/fit/" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
tensorboard_callback = tf.keras.callbacks.TensorBoard(log_dir=log_dir, histogram_freq=1)

# Then view with TensorBoard
# !tensorboard --logdir logs/fit

I implemented this approach for a fintech startup in New York that needed to visualize its credit scoring models, and it worked flawlessly while providing more interactive features.

Read AttributeError: Module ‘keras.optimizers’ has no attribute ‘sgd’

Method 4: Check Keras Version and Install Specific Version

Different versions of Keras have different module structures. You can check your Keras version:

import keras
print(keras.__version__)

I executed the above example code and added the screenshot below.

no module named 'keras.utils.np_utils'

Then install a specific version if needed:

pip uninstall keras tensorflow
pip install keras==2.3.1
pip install tensorflow==2.3.0

For a government project in Washington, D.C., analyzing census data, we had to downgrade to specific versions to maintain compatibility with their existing systems.

Check out AttributeError: Module ‘keras.optimizers’ has no attribute ‘sgd’

Method 5: Create a Workaround Function

If you’re working in an environment where you can’t change the package versions, you can create a workaround function:

def my_plot_model(model, to_file='model.png', show_shapes=True, show_layer_names=True):
    try:
        # Try the new import path first
        from tensorflow.keras.utils import plot_model
        plot_model(model, to_file, show_shapes, show_layer_names)
    except ImportError:
        try:
            # Try the old import path
            from keras.utils.vis_utils import plot_model
            plot_model(model, to_file, show_shapes, show_layer_names)
        except ImportError:
            # Fall back to just printing model summary
            print("Could not import plotting utilities. Printing summary instead:")
            model.summary()

I’ve used this approach in containerized applications for California-based tech companies where modifying the base image wasn’t an option.

Read AttributeError: Module ‘tensorflow’ has no attribute ‘dimension’

Method 6: Use the Keras-Specific Package

Sometimes the standalone Keras package works better:

pip uninstall keras tensorflow
pip install keras

Then try:

from keras.utils.vis_utils import plot_model

I’ve found this approach useful when working specifically with Keras models that don’t need the full TensorFlow ecosystem, like for a lightweight model deployed on AWS Lambda for a retail client in Seattle.

Check out AttributeError: module ‘tensorflow.keras.layers’ has no attribute ‘multiheadattention’

Method 7: Check for Path Issues with Graphviz

Sometimes the issue is related to Graphviz not being in your system path:

import os
os.environ["PATH"] += os.pathsep + 'C:/Program Files (x86)/Graphviz/bin/'  # Adjust for your path

This fixed the visualization problem for a team in Chicago who had correctly installed Graphviz but were still experiencing path-related errors.

Read Solve the ModuleNotFoundError: no module named ‘tensorflow_hub’

Troubleshoot Common Issues

If you’re still having problems, check these common issues:

  1. Conflicting installations: Multiple Python environments can cause conflicts.
   which python
   which pip
  1. Notebook vs Script: Sometimes code works in a script but not in a Jupyter notebook, or vice versa.
  2. Virtual environments: Make sure you’re installing packages in the correct environment.

Working with data scientists at a major US university, we discovered that their Anaconda installation was causing conflicts with system Python, leading to this error. Isolating the environments solved the problem.

Check out AttributeError: Module ‘keras.backend’ has no attribute ‘get_session’

The Future-Proof Solution

Based on my experience, the most reliable long-term solution is to use TensorFlow’s implementation of Keras:

from tensorflow import keras
from tensorflow.keras.utils import plot_model

model = keras.Sequential([
    keras.layers.Dense(64, input_shape=(10,)),
    keras.layers.Dense(1)
])

plot_model(model, to_file='model.png', show_shapes=True)

This approach has worked consistently across different projects and environments, including a recent machine learning pipeline for a Fortune 500 company in Texas.

Python libraries evolve, and keeping up with these changes is part of a developer’s journey. The keras.utils.vis_utils error is a perfect example of how library reorganization can affect our code. By using the solutions provided above, you should be able to resolve this error and continue building impressive deep learning models with proper visualization.

Remember to keep your libraries updated and check the official documentation when errors occur, as APIs may change between versions.

You may also read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.