'tensorflow' has no attribute 'config'
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
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.
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:
- upgrade the environment so it matches the code
- change the code so it matches the older TensorFlow stack
For older TensorFlow 1 style projects, the compatibility path often looks like this:
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.
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:
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.
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__, andhasattr(tf, "config")before changing code. - A missing
tf.configusually 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.v1configuration path instead.

