TensorFlow
MNIST
Python
Machine Learning
Tutorials

Error from tensorflow.examples.tutorials.mnist import input_data

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 import from tensorflow.examples.tutorials.mnist import input_data belongs to old TensorFlow 1.x tutorial code. In modern TensorFlow installations, that module is usually gone, which is why the import fails. The fix is normally to stop using the legacy helper and load MNIST with a current API such as tf.keras.datasets.mnist.

Why the Import Fails

The tensorflow.examples.tutorials.mnist package was a convenience module used in older tutorials. TensorFlow 2.x moved away from that layout, and many example helpers were removed rather than kept as stable public APIs.

So the failure is not usually a broken installation. It is usually a version mismatch between:

  • an old tutorial written for TensorFlow 1.x
  • a newer TensorFlow environment

The Modern Replacement

For MNIST, the simplest current loader is built into Keras.

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
8print(x_train.shape, y_train.shape)
9print(x_test.shape, y_test.shape)

This gives you the images and labels directly, without the old input_data.read_data_sets(...) wrapper.

Converting the Old Tutorial Pattern

Legacy code often looked like this:

python
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
batch_x, batch_y = mnist.train.next_batch(100)

In modern TensorFlow, a more standard approach is to use arrays or a tf.data.Dataset.

python
1import tensorflow as tf
2
3(x_train, y_train), _ = tf.keras.datasets.mnist.load_data()
4x_train = x_train.astype("float32") / 255.0
5
6dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train))
7dataset = dataset.shuffle(10000).batch(100)
8
9for batch_x, batch_y in dataset.take(1):
10    print(batch_x.shape, batch_y.shape)

This is the modern equivalent of "give me training batches," but it uses the current input pipeline tools instead of the removed tutorial helper.

What If You Must Run the Old Code

If you are trying to reproduce a historic TensorFlow 1.x notebook exactly, the realistic options are:

  • create a TensorFlow 1.x environment that matches the tutorial
  • or update the code to modern TensorFlow APIs

For ongoing work, updating the code is usually the better choice. Rebuilding an old environment makes sense only when exact historical reproduction matters.

It is also worth checking the rest of the tutorial around the import. Older examples often assume placeholder tensors, sessions, and manual training loops. Replacing only the dataset import can leave you with a half-migrated script that still depends on TensorFlow 1.x execution patterns elsewhere.

Why Updating Is Better

Modern TensorFlow expects different coding patterns:

  • eager execution by default
  • Keras as the standard model API
  • 'tf.data for input pipelines'

So even if you somehow restore the old import, you are still pulling a 1.x tutorial pattern into a 2.x world. That usually creates more migration problems later.

In practice, moving to the current loader usually simplifies the rest of the code too. Once the dataset is a normal NumPy tuple or a tf.data.Dataset, it becomes easier to integrate with modern Keras models, callbacks, metrics, and training loops.

Common Pitfalls

The most common mistake is assuming the import failed because TensorFlow was installed incorrectly, when the real issue is that the tutorial targets TensorFlow 1.x.

Another mistake is rewriting only the import line without updating the rest of the old batching and training code.

A third pitfall is forcing a legacy environment for new work instead of moving to tf.keras.datasets and tf.data.

Summary

  • 'tensorflow.examples.tutorials.mnist.input_data is a legacy TensorFlow 1.x helper.'
  • In modern TensorFlow, use tf.keras.datasets.mnist.load_data() instead.
  • Replace old next_batch(...) usage with arrays or a tf.data.Dataset pipeline.
  • Only recreate a TensorFlow 1.x environment if exact legacy reproduction is truly necessary.
  • For new code, migrate the tutorial rather than chasing the removed import.

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.