Python
TensorFlow
Keras
Machine Learning
Troubleshooting

No module named 'tensorflow.keras.layers.experimental.preprocessing'

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

The error No module named 'tensorflow.keras.layers.experimental.preprocessing' usually means your code was written for an older TensorFlow API layout. In newer TensorFlow and Keras releases, many preprocessing layers moved out of the experimental.preprocessing namespace and are imported directly from tensorflow.keras.layers.

Why the Import Fails

TensorFlow changed the location of several preprocessing layers as they matured from experimental APIs into stable ones. Code that once worked with imports such as:

python
from tensorflow.keras.layers.experimental.preprocessing import Normalization

may fail on newer versions because the layer now lives here:

python
from tensorflow.keras.layers import Normalization

So the problem is usually not that TensorFlow is broken. The code and the installed version simply do not agree about the module path.

Check the Installed Version First

Start by verifying the TensorFlow version that is actually running.

bash
python -c "import tensorflow as tf; print(tf.__version__)"

Then inspect the import in a Python shell:

python
import tensorflow as tf
print(tf.__version__)
from tensorflow.keras.layers import Normalization

If the direct import works, the fix is to update your source code to the newer namespace.

Use the Stable Import Path

For current TensorFlow versions, preprocessing layers are typically imported directly from tensorflow.keras.layers.

python
1import tensorflow as tf
2from tensorflow.keras import layers
3
4normalizer = layers.Normalization()
5resizer = layers.Resizing(224, 224)
6flipper = layers.RandomFlip("horizontal")

This is the version of the API you should prefer in modern code. It is cleaner, and it avoids dependency on a namespace that existed mainly during the transition period.

Example with a Small Model

The following example adapts a normalization layer and uses it inside a model. It uses the stable import style.

python
1import numpy as np
2import tensorflow as tf
3from tensorflow.keras import layers, Sequential
4
5x = np.array([[1.0, 2.0], [2.0, 4.0], [3.0, 6.0]], dtype="float32")
6y = np.array([0.0, 1.0, 1.0], dtype="float32")
7
8normalizer = layers.Normalization()
9normalizer.adapt(x)
10
11model = Sequential([
12    layers.Input(shape=(2,)),
13    normalizer,
14    layers.Dense(8, activation="relu"),
15    layers.Dense(1, activation="sigmoid"),
16])
17
18model.compile(optimizer="adam", loss="binary_crossentropy")
19model.fit(x, y, epochs=3, verbose=0)

That is the same general workflow older tutorials intended, but with imports that match current TensorFlow packaging.

When Downgrading Is Reasonable

If you are maintaining an old codebase that cannot be updated immediately, pinning TensorFlow to the version expected by the code can be a temporary workaround. That said, changing the import path is usually safer than downgrading the whole machine-learning stack, because version pinning can introduce compatibility issues with Python, CUDA, or other libraries.

A better long-term approach is:

  1. identify the current TensorFlow version
  2. update obsolete imports
  3. run the tests or training scripts again
  4. pin the working version in your project metadata

Keras Package Confusion

Another source of problems is mixing keras and tensorflow.keras imports in the same codebase. In some environments, that creates subtle version conflicts. If the project is TensorFlow-based, use one import style consistently.

For example, avoid mixing these in the same module:

python
from keras.layers import Dense
from tensorflow.keras.layers import Normalization

Stick to one stack unless the project has a deliberate reason to separate them.

Common Pitfalls

  • Assuming the missing module means TensorFlow was installed incorrectly often sends debugging in the wrong direction. In most cases the import path simply changed across versions.
  • Copying code from an older tutorial without checking the TensorFlow version causes avoidable namespace mismatches. Verify examples against the version you actually run.
  • Mixing keras and tensorflow.keras imports can create confusing compatibility problems. Use a consistent import source.
  • Downgrading TensorFlow without checking Python and dependency compatibility can create a larger environment problem than the original import error. Prefer updating the import path first.
  • Fixing one import while leaving other obsolete layer paths untouched leads to repeated failures. Review the full preprocessing API usage in the module.

Summary

  • The experimental.preprocessing namespace was moved in newer TensorFlow releases.
  • Current code should usually import preprocessing layers directly from tensorflow.keras.layers.
  • Check the TensorFlow version before deciding whether to update code or pin dependencies.
  • Keep keras and tensorflow.keras imports consistent within the project.
  • Updating the import path is usually the cleanest fix.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.