TensorFlow
MNIST
Python
import error
debugging

import input_data MNIST tensorflow not working

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

If import input_data for MNIST is not working, the usual reason is that you are following an old TensorFlow 1 tutorial in a newer TensorFlow environment. The old helper module under tensorflow.examples.tutorials.mnist is not the standard modern path anymore, so the fix is usually to switch to tf.keras.datasets.mnist or tensorflow_datasets.

Why the old import fails

Older TensorFlow 1 tutorials often used:

python
from tensorflow.examples.tutorials.mnist import input_data

In modern TensorFlow installs, that package path is usually unavailable or inappropriate because:

  • the examples module is no longer part of the expected TensorFlow 2 workflow
  • tutorials moved toward Keras datasets
  • many environments do not ship the old helper module at all

So the failure is usually not a broken MNIST dataset. It is an outdated import path.

Modern replacement: tf.keras.datasets.mnist

The current standard way to load MNIST in TensorFlow 2 is:

python
1import tensorflow as tf
2
3(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
4
5print(x_train.shape, y_train.shape)
6print(x_test.shape, y_test.shape)

This downloads the dataset if needed and returns NumPy arrays directly.

For model training, normalize and add the channel dimension if required:

python
1import tensorflow as tf
2
3(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
4
5x_train = x_train.astype("float32") / 255.0
6x_test = x_test.astype("float32") / 255.0
7
8x_train = x_train[..., tf.newaxis]
9x_test = x_test[..., tf.newaxis]
10
11print(x_train.shape)

That shape change is often necessary for convolutional models.

Alternative: tensorflow_datasets

If you prefer a dataset pipeline rather than raw arrays, tensorflow_datasets is a good option.

python
1import tensorflow_datasets as tfds
2
3ds_train, ds_test = tfds.load(
4    "mnist",
5    split=["train", "test"],
6    as_supervised=True,
7)
8
9for image, label in ds_train.take(1):
10    print(image.shape, label.numpy())

This integrates naturally with tf.data workflows.

Full TensorFlow 2 example

Here is a minimal end-to-end TensorFlow 2 training example using the modern loader:

python
1import tensorflow as tf
2
3(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
4
5x_train = x_train.astype("float32") / 255.0
6x_test = x_test.astype("float32") / 255.0
7
8model = tf.keras.Sequential([
9    tf.keras.layers.Flatten(input_shape=(28, 28)),
10    tf.keras.layers.Dense(128, activation="relu"),
11    tf.keras.layers.Dense(10),
12])
13
14model.compile(
15    optimizer="adam",
16    loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
17    metrics=["accuracy"],
18)
19
20model.fit(x_train, y_train, epochs=1, validation_data=(x_test, y_test))

If this works, the original issue was the old import path rather than the dataset itself.

If you must run legacy TensorFlow 1 code

If the project truly depends on old TensorFlow 1 examples, the real answer may be environment isolation rather than trying to force modern TensorFlow 2 into old tutorial imports. In that case:

  • create a dedicated legacy virtual environment
  • pin the required TensorFlow version
  • document clearly that the code is tutorial-era TensorFlow 1 style

That is usually cleaner than mixing old and new APIs in one environment.

Common Pitfalls

The most common mistake is searching for a workaround that keeps the old tensorflow.examples.tutorials.mnist import alive in a modern TensorFlow 2 setup. Another is loading MNIST successfully but forgetting to normalize pixel values before training. Developers also often forget the channel dimension needed for convolutional models and then blame the dataset loader. Network restrictions can be another issue if the dataset download fails on a locked-down machine, but that is a different problem from the missing import path. Finally, some users mix TensorFlow 1 session-style code with TensorFlow 2 eager-style data loading, which creates unnecessary confusion.

Summary

  • The old input_data MNIST import is usually a TensorFlow 1 era pattern.
  • In TensorFlow 2, use tf.keras.datasets.mnist.load_data() instead.
  • Use tensorflow_datasets if you want a tf.data style pipeline.
  • Normalize images and adjust shape to match the model you plan to train.
  • If legacy code must be preserved, isolate it in a version-pinned old environment.
  • Treat missing old imports as a compatibility issue, not a sign that MNIST itself is broken.

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.