Cannot import name 'tf_utils' when using importing keras
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
The Cannot import name 'tf_utils' error usually appears when a project mixes incompatible TensorFlow and Keras packages. In older setups, developers installed the standalone keras package directly. In modern TensorFlow workflows, keras is bundled inside TensorFlow as tf.keras. If your environment has partially upgraded dependencies, Python may load modules from both worlds, and import paths that once worked can fail. The good news is that this is typically an environment consistency problem, not a model logic problem. Once imports and versions are aligned, the error goes away and stays away.
Why the tf_utils ImportError Happens
Python resolves imports based on what is installed first on sys.path. If you install tensorflow, then later install a conflicting keras version, you can end up with internal files expecting different module layouts.
A common anti-pattern looks like this:
The third line is especially fragile because tf_utils is an internal utility module in many versions and is not guaranteed as a public API. Even when it exists, its location can move between releases.
You can quickly inspect the environment that Python is actually loading:
If keras.__file__ points to a standalone package while your app expects tf.keras, that mismatch is the likely cause.
Use Compatible Imports and Versions
For TensorFlow 2.x projects, prefer tf.keras imports end to end. This avoids dependency drift and aligns with the TensorFlow release cadence.
If your code or a third-party library imports private internals such as keras.utils.tf_utils, refactor to public APIs. Private modules are version-sensitive and can break during minor upgrades.
When pinning versions, keep them simple and explicit:
In most projects, installing only tensorflow is enough because it already includes Keras.
Rebuild a Clean Virtual Environment
If the environment has been modified repeatedly, a clean rebuild is faster than chasing partial conflicts.
Then validate imports before running your app:
If this works in a fresh environment but fails in your project environment, the issue is almost certainly package contamination.
For reproducible teams, commit a lock file (requirements.txt, poetry.lock, or uv.lock) and rebuild environments from scratch in CI.
When Standalone Keras Is Required
Some legacy projects intentionally use standalone Keras. In that case, keep TensorFlow and Keras versions explicitly compatible and avoid mixing import styles.
The key rule is consistency. Either use TensorFlow-integrated Keras everywhere or standalone Keras everywhere in that codebase.
Common Pitfalls
- Importing private modules such as
keras.utils.tf_utilsdirectly instead of public APIs. - Mixing
from keras...andfrom tensorflow.keras...in the same package. - Upgrading one dependency in place without recreating the virtual environment.
- Running notebooks from a different interpreter than the one where dependencies were installed.
- Assuming the latest package versions are mutually compatible without pinning and testing.
Summary
Cannot import name 'tf_utils' is usually a dependency alignment issue caused by mixed Keras/TensorFlow installations or reliance on private internals. Standardize on tf.keras for TensorFlow 2.x, rebuild a clean environment, and pin versions to keep installs reproducible. Once your imports are consistent and public-API based, this error is typically resolved permanently.
Related reading
- Cannot import Tensorflow for GPU on Windows 10
- Cannot import tensorflow in python after source build
- Cannot install TensorFlow 1.x
- Cannot load tensorflow_hub
- Cannot run tensorflow on GPU
- Cannot Split Malaria Dataset using Tensorflow Datasets
- Cannot install Lxml on Mac OS X 10.9
- Cannot pickle Tensorflow object in Python - TypeError can't pickle _thread._local objects
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.