ImportError cannot import name 'to_categorical' from 'keras.utils' /usr/local/lib/python3.7/dist-packages/keras/utils/__init__.py
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
This import error usually means your environment has conflicting Keras package paths or versions. In modern TensorFlow projects, the safest import is from tensorflow.keras, not standalone keras. A systematic environment check fixes the issue faster than repeated reinstall attempts.
Why the Error Happens
Historically, Keras existed both as standalone package and as TensorFlow-integrated API. Mixing both in one environment can make imports resolve to unexpected modules where to_categorical is missing or relocated.
Common problematic line:
Preferred for TensorFlow workflows:
Use one namespace consistently across the project.
Verify Active Interpreter and Package Paths
Check interpreter and pip target first.
Then inspect import locations from Python:
If keras path points to an unexpected global site-packages folder, environment contamination is likely.
Clean Virtual Environment Fix
A fresh environment is often the fastest reliable solution.
Test import immediately:
If this works, old environment state was the root problem.
Migrate Legacy Imports Safely
If codebase mixes keras.* and tensorflow.keras.*, do a focused migration PR:
- Replace imports to one namespace.
- Run unit and smoke training tests.
- Compare output tensor shapes before and after.
Keep migration separated from architecture changes so regressions are easier to trace.
Notebook and Colab Gotchas
In notebook runtimes, package changes may not apply until kernel restart.
Recommended flow:
- Install dependencies.
- Restart runtime.
- Re-run imports from top.
Without restart, stale modules in memory can keep raising the same error even after correct installation.
End-to-End Validation Script
Run a tiny training pipeline to confirm import and functionality.
This confirms import path, tensor conversion, and basic model compatibility.
Version and Locking Strategy
To avoid recurrence:
- Pin TensorFlow version in requirements or lock file.
- Avoid installing standalone keras unless your project explicitly requires it.
- Use one environment per project.
Reproducible environments are more important than ad hoc fixes.
A practical lock strategy is to pin TensorFlow major and minor version, then update intentionally through review so namespace shifts are caught in CI rather than in ad hoc notebook sessions.
Add a Fast Environment Sanity Check
You can fail early in CI with a tiny smoke script that verifies import path and runtime versions.
Running this before training jobs catches broken environments quickly and prevents expensive long-running tasks from failing late.
Common Pitfalls
- Mixing
kerasandtensorflow.kerasimports in one project. - Installing packages into a different interpreter than execution runtime.
- Reusing polluted global Python environments for ML workloads.
- Skipping notebook runtime restart after dependency changes.
- Migrating imports and model logic in the same change, obscuring root causes.
Summary
- This error is usually an environment and namespace consistency problem.
- Prefer
tensorflow.keras.utils.to_categoricalin TensorFlow-based projects. - Validate interpreter, pip target, and installed package paths first.
- Use clean virtual environments for deterministic behavior.
- Keep imports consistent and dependencies pinned to prevent repeat failures.

