ImportError cannot import name 'get_config' from 'tensorflow.python.eager.context'
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
This import error usually points to a version mismatch inside the TensorFlow stack rather than a bug in your own model code. In most cases, something in the environment is importing a private TensorFlow module path, or a package built against one TensorFlow release is running against another.
Why This Error Happens
The path tensorflow.python.eager.context is an internal TensorFlow implementation detail. Anything under tensorflow.python is considered private API. Private symbols can move or disappear between releases, so code that imports them directly is fragile.
The specific failure around get_config often happens in one of these situations:
- A third-party package expects an older TensorFlow layout.
- '
keras,tensorflow,tensorflow-estimator, or another related package is installed at incompatible versions.' - The environment contains leftovers from multiple installs, such as a global package mixed with a virtual environment.
If your own code imports from tensorflow.python, start by removing that dependency. Public TensorFlow APIs are much more stable.
Verify the Installed Versions
Before changing packages, inspect what is actually installed:
Then test a minimal import:
If this minimal script fails before your application code runs, the issue is in the environment, not in your model logic.
It is also useful to see which interpreter pip is writing to:
Those two commands should point to the same environment.
Stop Importing Private TensorFlow Modules
A direct import like this is risky:
Prefer public APIs instead. In many cases, you do not need get_config at all. If your goal is to check eager mode or device settings, use public helpers:
If a third-party library is the one importing tensorflow.python.eager.context, update that library first. Libraries that depend on private TensorFlow symbols tend to break when TensorFlow changes internally.
Rebuild the Environment Cleanly
When TensorFlow package conflicts appear, a clean virtual environment is often faster than trying to repair the old one piecemeal.
After that, test the import again:
If you also need other TensorFlow-adjacent packages such as tensorflow-probability or standalone keras, install them only after confirming the base TensorFlow import works.
Check Companion Packages Carefully
The hardest cases happen when TensorFlow itself is fine, but a companion package expects a different internal API. That is why version alignment matters. Install related packages together and avoid pinning one package to a very old release while letting another float to a newer one.
A practical workflow is:
- Create a fresh environment.
- Install only TensorFlow.
- Verify
import tensorflowworks. - Add one dependent package at a time.
- Re-test after each install.
That sequence tells you which package introduces the mismatch.
Common Pitfalls
The most common mistake is importing private modules directly because they appear in examples or old answers online. If the path begins with tensorflow.python, assume it is unstable.
Another problem is mixing pip install keras with a TensorFlow version that expects a different Keras integration story. If you are unsure, start with TensorFlow alone and add extra packages only when you know they are compatible.
A dirty environment is another frequent cause. Reinstalling TensorFlow over an existing environment can leave behind incompatible files, especially if the environment previously held a different major version.
Finally, do not debug model code before confirming that a one-line import tensorflow as tf succeeds in a fresh environment.
Summary
- This error usually comes from version mismatch or private TensorFlow imports.
- Avoid importing anything from
tensorflow.pythonin application code. - Check the installed
tensorflowandkeraspackages in the active interpreter. - Recreate the environment cleanly if the minimal import already fails.
- Add dependent packages back one at a time to identify the conflicting dependency.
Related reading
- ImportError cannot import name 'ImageDataGenerator' from 'keras.preprocessing.image
- ImportError cannot import name 'keras
- ImportError cannot import name 'keras_tensor' from 'tensorflow.python.keras.engine
- ImportError cannot import name 'LayerNormalization' from 'tensorflow.python.keras.layers.normalization
- ImportError cannot import name 'joblib' from 'sklearn.externals
- ImportError cannot import name 'to_categorical' from 'keras.utils' /usr/local/lib/python3.7/dist-packages/keras/utils/__init__.py
- ImportError cannot import name 'set_random_seed' from 'tensorflow' CUserspolonAnaconda3libsite-packagestensorflow__init__.py
- ImportError cannot import name 'url' from 'django.conf.urls' after upgrading to Django 4.0
.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.