Python
Keras
Conv2D
ImportError
Deep Learning

keras - cannot import name Conv2D

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

ImportError: cannot import name 'Conv2D' usually means your import path does not match the Keras package you actually installed. In modern environments, the safest approach is to use either tf.keras.layers.Conv2D from TensorFlow or the current standalone Keras layers API consistently, and to avoid mixing old keras and tensorflow packages in the same environment.

Use an Import Style That Matches the Installed Stack

If your project is based on TensorFlow's bundled Keras, this import is the most reliable:

python
1from tensorflow.keras.layers import Conv2D
2from tensorflow.keras.models import Sequential
3
4model = Sequential([
5    Conv2D(32, kernel_size=(3, 3), activation="relu", input_shape=(28, 28, 1))
6])

If you are using the current standalone Keras package, the modern style is usually:

python
1from keras import layers
2from keras import Sequential
3
4model = Sequential([
5    layers.Conv2D(32, kernel_size=(3, 3), activation="relu", input_shape=(28, 28, 1))
6])

Pick one stack and stay consistent. Do not import half the project from keras and the other half from tensorflow.keras unless you know exactly why.

Check What Is Actually Installed

Many import problems come from environment confusion rather than bad code. Check which interpreter and packages the script is using.

bash
python -c "import sys; print(sys.executable)"
python -m pip show tensorflow keras
python -m pip list | grep -E 'tensorflow|keras'

If one notebook kernel, one virtual environment, or one IDE terminal uses a different interpreter than the shell where you installed packages, the import error is expected.

Remove Naming Conflicts in Your Project

A local file or folder can shadow the real Keras package. Common bad names are:

  • 'keras.py'
  • 'tensorflow.py'
  • a folder named keras
  • stale __pycache__ content after renaming

This short diagnostic helps:

bash
python -c "import keras; print(keras.__file__)"

If the printed path points into your project instead of the installed package location, the import is being shadowed locally.

Rebuild a Clean Environment When in Doubt

When package versions have drifted, cleaning the environment is often faster than chasing import side effects.

bash
1python -m venv .venv
2source .venv/bin/activate
3python -m pip install --upgrade pip
4python -m pip install tensorflow

Then run a minimal check:

bash
1python - <<'PY'
2from tensorflow.keras.layers import Conv2D
3print(Conv2D)
4PY

If that works in a clean environment, the issue was environment state, not the layer itself.

Avoid Old Examples Without Checking the Version Context

A lot of older blog posts were written against different Keras packaging layouts. Some use imports that were valid in older standalone releases, while current TensorFlow documentation centers tf.keras.layers.Conv2D and current Keras documentation exposes the layer under the Keras layers API.

The practical lesson is simple: import examples without version context are not trustworthy by themselves.

Minimal Working Example

Once the import is correct, a tiny CNN should run immediately.

python
1from tensorflow.keras import Sequential
2from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
3
4model = Sequential([
5    Conv2D(16, (3, 3), activation="relu", input_shape=(28, 28, 1)),
6    MaxPooling2D(pool_size=(2, 2)),
7    Flatten(),
8    Dense(10, activation="softmax"),
9])
10
11model.summary()

If this fails at the import line, the problem is installation or module resolution. If the import succeeds but model creation fails, you are now debugging a different issue.

Common Pitfalls

One common mistake is installing both standalone keras and TensorFlow in a partially compatible combination and then mixing their imports. Another is trusting an IDE interpreter that is different from the environment where packages were installed.

Local filename shadowing is also surprisingly common. A file named keras.py can waste a lot of time.

Finally, do not copy imports from an old tutorial without checking whether it targeted standalone Keras, tf.keras, or a specific TensorFlow release.

Summary

  • Use from tensorflow.keras.layers import Conv2D for TensorFlow-based projects.
  • Use the current Keras layers API only if your environment is intentionally built around standalone Keras.
  • Verify the active interpreter and installed packages before changing code blindly.
  • Check for local file or folder names that shadow the real package.
  • When the environment is messy, a clean virtual environment is often the fastest 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.