Python
Keras
Machine Learning
Deep Learning
Debugging

AttributeError module 'keras.utils' has no attribute 'Sequence'

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

AttributeError: module 'keras.utils' has no attribute 'Sequence' usually means the code and the installed package layout do not match. In most real environments the bug is not that Sequence disappeared completely, but that the project mixes standalone keras imports with tensorflow.keras imports or uses an unexpected package version.

Why the Error Happens

There are two common Keras import styles in Python projects:

  • standalone keras
  • 'tensorflow.keras'

They look similar, but they are not always interchangeable. A tutorial written for one layout may fail in an environment that has the other. That is why copying one line such as from keras.utils import Sequence can work on one machine and fail on another.

Notebook environments make this worse because package upgrades do not always reset the running kernel state.

Inspect the Installed Environment First

Before editing model code, check what is actually installed.

python
1import importlib
2import pkgutil
3
4print("tensorflow installed:", pkgutil.find_loader("tensorflow") is not None)
5print("keras installed:", pkgutil.find_loader("keras") is not None)
6
7for module_name in ["keras.utils", "tensorflow.keras.utils"]:
8    try:
9        module = importlib.import_module(module_name)
10        print(module_name, "has Sequence:", hasattr(module, "Sequence"))
11    except Exception as exc:
12        print(module_name, type(exc).__name__, exc)

This tells you which namespace is valid in the current environment instead of guessing.

Use One Namespace Consistently

For TensorFlow-centric projects, the safest default is usually tensorflow.keras everywhere.

python
1import numpy as np
2import tensorflow as tf
3from tensorflow.keras.utils import Sequence
4
5
6class ToySequence(Sequence):
7    def __init__(self, x, y, batch_size=4):
8        self.x = np.asarray(x, dtype="float32")
9        self.y = np.asarray(y, dtype="float32")
10        self.batch_size = batch_size
11
12    def __len__(self):
13        return int(np.ceil(len(self.x) / self.batch_size))
14
15    def __getitem__(self, index):
16        start = index * self.batch_size
17        end = start + self.batch_size
18        return self.x[start:end], self.y[start:end]
19
20
21x = np.random.rand(16, 3)
22y = np.random.randint(0, 2, size=(16, 1))
23
24model = tf.keras.Sequential([
25    tf.keras.layers.Input(shape=(3,)),
26    tf.keras.layers.Dense(8, activation="relu"),
27    tf.keras.layers.Dense(1, activation="sigmoid"),
28])
29model.compile(optimizer="adam", loss="binary_crossentropy")
30model.fit(ToySequence(x, y), epochs=1, verbose=0)

That example works because the model, layers, and Sequence base class all come from the same API family.

Do Not Mix Imports Across the Project

A codebase becomes fragile when it does things like:

  • 'from keras.utils import Sequence'
  • 'from tensorflow.keras.models import Model'
  • 'from keras.layers import Dense'

Sometimes this appears to work until a deployment or upgrade changes the package resolution order. Then the same code starts failing in ways that look arbitrary.

A better migration strategy is to normalize the whole project at once:

  1. choose one namespace
  2. update every import in the repository
  3. rebuild the environment
  4. rerun training and inference tests

Partial migration is how this error keeps returning.

Version Pinning Matters

This is also a dependency-management problem. If one developer has old standalone Keras and another has a newer TensorFlow bundle, you do not really have one environment.

Pin versions in requirements.txt or pyproject.toml, recreate virtual environments after major upgrades, and restart notebook kernels after package changes. Those boring steps prevent most of the confusion around missing Keras attributes.

Common Pitfalls

  • Mixing keras and tensorflow.keras imports in the same project.
  • Debugging model code before checking the installed package versions.
  • Upgrading packages in a notebook and not restarting the kernel.
  • Applying a one-line import fix in one file while the rest of the codebase still uses the old namespace.

Summary

  • This error usually comes from incompatible Keras import paths, not from Sequence being conceptually unavailable.
  • Inspect the environment first so you know which namespace is valid.
  • In TensorFlow projects, tensorflow.keras is the safer default.
  • Migrate imports consistently across the project instead of patching one file.
  • Lock versions and rebuild environments to keep the error from returning.

Course illustration
Course illustration

All Rights Reserved.