Cannot import name 'load_img' from 'keras.preprocessing.image'
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The Cannot import name 'load_img' from 'keras.preprocessing.image' error usually comes from version mismatch between standalone Keras and TensorFlow-integrated Keras. The import path changed across releases, so old tutorials can fail in modern environments. The fix is selecting the import path that matches your installed package versions.
Use the Correct Import for Your Stack
In most current TensorFlow workflows, import from tensorflow.keras.utils:
For some environments, tensorflow.keras.preprocessing.image may still work, but utils is a safer modern default for portability across recent releases.
Minimal image load example:
If this succeeds, your import path is aligned with installed TensorFlow.
Diagnose Version Conflicts Quickly
Many failures happen because keras and tensorflow packages are installed with incompatible versions. Check what is actually installed in the active environment.
If both packages exist but versions differ significantly, choose one API style and align dependencies.
Prefer One Keras Source in a Project
Mixing imports like from keras... and from tensorflow.keras... in the same codebase can cause subtle runtime issues, including incompatible object types and serialization errors.
Pick one convention:
- TensorFlow-centric project: use
tensorflow.kerasimports everywhere - standalone Keras project: use
kerasimports consistently and pin matching backend setup
Example cleanup:
Consistency reduces debugging time when moving between training and serving scripts.
Rebuild a Clean Environment if Needed
When dependency drift is severe, rebuilding the virtual environment is often faster than patching packages in place.
Then run a smoke test:
Lock versions in a dependency file so teammates and CI use the same stack.
Legacy Code Migration Pattern
If you inherit older notebooks, search and replace outdated imports carefully.
Common migration:
- old:
from keras.preprocessing.image import load_img - new:
from tensorflow.keras.utils import load_img
After migration, rerun preprocessing pipelines and verify output shapes and dtypes. Import fixes can still hide behavior changes if downstream defaults differ.
Common Pitfalls
- Installing both
kerasandtensorflowwithout version alignment. - Mixing standalone Keras imports with TensorFlow Keras imports in one script.
- Assuming tutorial import paths from older releases still apply unchanged.
- Updating one package only and leaving stale transitive dependencies.
- Running code in a different interpreter than the one where packages were installed.
Summary
- The
load_imgimport error is usually a package-version path mismatch. - For modern TensorFlow workflows, use
tensorflow.keras.utilsimports. - Check installed versions and keep one consistent Keras API style.
- Rebuild virtual environments when dependency drift becomes hard to reason about.
- Pin versions and run smoke tests to prevent recurring import regressions.
- Add a small startup diagnostic script in CI that verifies critical imports before model training begins.
- Keep notebook and production environments synced with one lock file to avoid hidden package drift.
- Re-test image preprocessing output after migration to confirm unchanged data semantics.

