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

The error AttributeError: 'tensorflow' has no attribute 'config' usually means one of two things: the environment is using an older TensorFlow API, or Python did not actually import the real TensorFlow package at all. The fastest fix is to stop guessing and inspect exactly what import tensorflow as tf loaded.

Check What Was Imported

Start with three values: the TensorFlow version, the module path, and whether the attribute exists.

python
1import tensorflow as tf
2
3print(tf.__version__)
4print(hasattr(tf, "config"))
5print(tf.__file__)

Those lines answer three important questions:

  • is this a TensorFlow version new enough for the code you wrote
  • does the imported module expose config
  • where did Python import the module from

If the path is not inside the expected environment, or the version is much older than the code assumes, the error makes sense immediately.

Version Mismatch Is The Most Common Cause

tf.config belongs to newer TensorFlow APIs. If the project is running against an older TensorFlow installation, that namespace may not exist.

In that case, you have two realistic choices:

  1. upgrade the environment so it matches the code
  2. change the code so it matches the older TensorFlow stack

For older TensorFlow 1 style projects, the compatibility path often looks like this:

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

That is not a drop-in replacement for every tf.config call, but it is the right direction if the project is intentionally pinned to older APIs.

Watch For Local Module Shadowing

Sometimes TensorFlow is installed correctly and the problem is a local file named tensorflow.py or a folder named tensorflow in your project. Python then imports that local module instead of the real package.

python
import tensorflow as tf

print(tf.__file__)

If the printed path points into your project folder rather than into the environment's site-packages directory, rename the local file or folder and remove stale __pycache__ artifacts.

This kind of shadowing is easy to miss because the import statement itself still succeeds.

Confirm The Active Interpreter

A surprisingly common situation is that the shell, IDE, notebook kernel, and test runner all point at different Python interpreters. You may install TensorFlow in one environment and run code from another.

Printing the Python executable alongside the TensorFlow path makes this visible:

python
1import sys
2import tensorflow as tf
3
4print(sys.executable)
5print(tf.__file__)

If those paths do not match the environment you thought you were using, fix the interpreter selection before touching the code.

Use tf.config Only After The Import Is Correct

Once you confirm that the correct TensorFlow package is loaded, tf.config is the right place for runtime configuration such as GPU visibility or memory growth.

python
1import tensorflow as tf
2
3gpus = tf.config.list_physical_devices("GPU")
4for gpu in gpus:
5    tf.config.experimental.set_memory_growth(gpu, True)
6
7print(gpus)

This code is fine only after the environment issue is resolved. Otherwise you risk debugging runtime configuration when the import itself is wrong.

Common Pitfalls

  • Running TensorFlow 2 style code in an environment that still has older TensorFlow APIs.
  • Assuming the import is correct without checking tf.__file__.
  • Shadowing TensorFlow with a local file or package named tensorflow.
  • Installing the package into one interpreter and running code from another.
  • Mixing old compatibility APIs and new runtime APIs without deciding which TensorFlow generation the project targets.

Summary

  • Check tf.__version__, tf.__file__, and hasattr(tf, "config") before changing code.
  • A missing tf.config usually means a version mismatch or a bad import target.
  • Local module shadowing and wrong interpreter selection are common causes.
  • If the project must stay on older TensorFlow APIs, use the tf.compat.v1 configuration path instead.

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.