Python
TensorFlow
AttributeError
Debugging
Machine Learning

'tensorflow' has no attribute 'config'

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

AttributeError: module 'tensorflow' has no attribute 'config' usually points to one of two problems: you are running TensorFlow 1.x code against a TensorFlow 2.x API example, or Python is importing the wrong module because a local file or package named tensorflow is shadowing the real library.

First Check the Imported Module

Before changing code, verify what Python actually imported:

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

If tf.__file__ points to your project directory rather than the installed package location, you probably have a local file such as tensorflow.py or a folder named tensorflow shadowing the real library.

That is one of the most common causes of mysterious missing attributes.

tf.config Is a TensorFlow 2 API

The tf.config namespace is part of TensorFlow 2.x and is commonly used for GPU discovery, memory growth, and device configuration.

For example:

python
import tensorflow as tf

print(tf.config.list_physical_devices("GPU"))

If you are actually running TensorFlow 1.x, that attribute will not exist. In that case, the fix is either:

  • upgrade to TensorFlow 2 if your code expects TensorFlow 2 APIs
  • rewrite the code to use TensorFlow 1 compatible APIs

Version Mismatch in Practice

Check the installed version with:

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

If the version is 1.x and the code uses tf.config, you have a version mismatch. Modern examples assume TensorFlow 2, while older environments may still be pinned to TensorFlow 1 for legacy reasons.

In TensorFlow 1.x, device and session configuration often lived in different APIs such as ConfigProto:

python
1import tensorflow as tf
2
3config = tf.compat.v1.ConfigProto()
4config.gpu_options.allow_growth = True

That is not a direct replacement for every tf.config call, but it shows why old and new examples can diverge so sharply.

Environment Problems Are Also Common

Another frequent cause is using the wrong virtual environment. You may have installed TensorFlow 2 in one environment and be running the script with a different interpreter.

That is why checking all three of these together is important:

  • 'python --version'
  • 'which python or where python'
  • 'pip show tensorflow'

If they do not line up, you may be installing TensorFlow into one interpreter and running another.

This is especially common in notebook environments, IDEs, and machines that have both system Python and one or more virtual environments installed.

A Good Debugging Flow

Use this order:

  1. print tf.__file__ to rule out module shadowing
  2. print tf.__version__ to confirm the installed TensorFlow major version
  3. confirm the active interpreter and environment
  4. decide whether the code should target TensorFlow 1 compatibility or TensorFlow 2 APIs

That sequence usually resolves the error quickly.

Common Pitfalls

  • Renaming nothing and repeatedly reinstalling TensorFlow when the real problem is a local tensorflow.py file.
  • Assuming all TensorFlow tutorials use the same major version of the API.
  • Mixing TensorFlow 1 compatibility code with TensorFlow 2 device-configuration examples.
  • Installing TensorFlow into one environment and running the script from another.
  • Fixing the import error without checking whether the rest of the codebase expects TensorFlow 1 or TensorFlow 2 behavior.

Summary

  • 'tf.config is a TensorFlow 2 API, so the error often means you are not actually running TensorFlow 2.'
  • A shadowing local module named tensorflow can produce the same symptom.
  • Print tf.__version__ and tf.__file__ before making bigger changes.
  • Use TensorFlow 1 compatibility APIs only if the project genuinely targets that older runtime.
  • Most fixes come down to version alignment or import-path cleanup, not to exotic TensorFlow bugs.

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.