Python
TensorFlow
ImportError
Troubleshooting
Machine Learning

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.

Practice ML system design

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:

bash
python -m pip show tensorflow keras tensorflow-estimator
python -m pip list | grep -E 'tensor|keras'

Then test a minimal import:

python
1import tensorflow as tf
2
3print(tf.__version__)
4print(tf.executing_eagerly())

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:

bash
python -c "import sys; print(sys.executable)"
python -m pip --version

Those two commands should point to the same environment.

Stop Importing Private TensorFlow Modules

A direct import like this is risky:

python
from tensorflow.python.eager.context import get_config

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:

python
1import tensorflow as tf
2
3print(tf.executing_eagerly())
4print(tf.config.list_physical_devices())

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.

bash
1python -m venv .venv
2source .venv/bin/activate
3python -m pip install --upgrade pip setuptools wheel
4python -m pip install tensorflow

After that, test the import again:

bash
python -c "import tensorflow as tf; print(tf.__version__)"

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:

  1. Create a fresh environment.
  2. Install only TensorFlow.
  3. Verify import tensorflow works.
  4. Add one dependent package at a time.
  5. 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.python in application code.
  • Check the installed tensorflow and keras packages 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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Practice ML system design

All Rights Reserved.