Python
TensorFlow
ModuleNotFoundError
Machine Learning
Troubleshooting

ModuleNotFoundError No module named 'tensorflow.python.training'

Master System Design with Codemia

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

Introduction

ModuleNotFoundError: No module named 'tensorflow.python.training' usually means your code is importing a TensorFlow internal module instead of a supported public API. It can also happen when TensorFlow is missing, partially installed, or when an older package expects an internal path that no longer exists in your environment.

Prefer Public TensorFlow Imports

TensorFlow examples and guides use the public package import:

python
import tensorflow as tf

print(tf.__version__)

That import is stable across supported releases. By contrast, tensorflow.python.* is an internal implementation namespace. Code that imports directly from it may work in one release and break in another.

If your code currently looks like this:

python
from tensorflow.python.training import checkpoint_management

replace it with a public API when possible. For checkpointing, modern TensorFlow code typically uses tf.train.Checkpoint or Keras model saving.

python
1import tensorflow as tf
2
3model = tf.keras.Sequential([
4    tf.keras.layers.Dense(8, activation="relu", input_shape=(4,)),
5    tf.keras.layers.Dense(1)
6])
7
8optimizer = tf.keras.optimizers.Adam()
9checkpoint = tf.train.Checkpoint(model=model, optimizer=optimizer)
10save_path = checkpoint.save("/tmp/demo_ckpt")
11print(save_path)

Verify the Environment Before Refactoring Further

If the public import itself fails, the problem is more basic: TensorFlow is not installed in the interpreter you are running. Confirm the interpreter and package path first.

bash
python -m pip show tensorflow
python -c "import sys; print(sys.executable)"
python -c "import tensorflow as tf; print(tf.__version__)"

If pip show returns nothing, install TensorFlow into the active environment and retry. Running python -m pip is safer than calling pip directly because it ties installation to the same interpreter you are about to use.

bash
python -m pip install --upgrade pip
python -m pip install tensorflow

Handle Legacy TensorFlow 1 Code Carefully

Some older tutorials and third-party packages reached into TensorFlow internals because TensorFlow 1 code often relied on implementation details. If you are migrating that code, move toward public APIs under tf, tf.keras, or tf.compat.v1 instead of trying to preserve every old internal import.

For example, a legacy training utility may be better rewritten around Keras callbacks:

python
1import tensorflow as tf
2
3model = tf.keras.Sequential([
4    tf.keras.layers.Input(shape=(4,)),
5    tf.keras.layers.Dense(16, activation="relu"),
6    tf.keras.layers.Dense(1)
7])
8
9model.compile(optimizer="adam", loss="mse")
10
11callback = tf.keras.callbacks.ModelCheckpoint(
12    filepath="/tmp/model.weights.h5",
13    save_weights_only=True,
14)

If the failing import comes from a dependency rather than your own code, update that dependency first. A package pinned to an outdated TensorFlow internals layout may never be reliable on a modern installation.

It also helps to isolate the problem in a clean virtual environment. If the same script works there, your original environment likely contains conflicting packages, old editable installs, or an interpreter mismatch rather than a real TensorFlow API issue.

Common Pitfalls

The biggest mistake is trying to "fix" the error by hunting for another internal tensorflow.python.* path. That treats the symptom, not the cause. Internal imports are not a stable compatibility contract.

Another problem is mixing environments. You may install TensorFlow in one virtual environment and run the script from another interpreter in your IDE, notebook kernel, or shell. Always verify sys.executable when debugging import issues.

Partial installs can also cause confusing behavior. If TensorFlow installation was interrupted, uninstall and reinstall cleanly rather than assuming the package tree is intact.

Finally, if a notebook still shows the same error after installation, restart the kernel. Long-lived Python sessions often keep stale import state.

Summary

  • 'tensorflow.python.training is an internal path and should not be treated as a public API.'
  • Prefer import tensorflow as tf and public APIs such as tf.train.Checkpoint or Keras callbacks.
  • Verify that TensorFlow is installed in the exact interpreter that runs your code.
  • Update old dependencies that import TensorFlow internals instead of patching around them.
  • Restart notebook kernels or shells after installation changes when the error persists.

Course illustration
Course illustration

All Rights Reserved.