Recently, I was working on a machine learning project using TensorFlow when I encountered an error that stopped me in my tracks: “AttributeError: Module ‘tensorflow’ has no attribute ‘logging’“. This error usually occurs when attempting to access TensorFlow’s logging functionality that may not exist in your current version or has been relocated.
In this article, I will share several solutions to fix this error based on my experience with different versions of TensorFlow.
Whether you are building a simple neural network or creating a complex deep learning model, these solutions will help you quickly get back on track.
Understand the Error
The error “Module ‘tensorflow’ has no attribute ‘logging'” typically appears when you try to use tensorflow.logging in your code, but this attribute is no longer available in your version of TensorFlow. This commonly happens when:
- You’re using code written for TensorFlow 1.x in a TensorFlow 2.x environment
- You’ve recently upgraded your TensorFlow version
- You’re following outdated tutorials or documentation
Let’s dive into the solutions that have worked for me.
Read ModuleNotFoundError: No module named tensorflow Keras
Method 1: Use tf.compat.v1.logging
TensorFlow 2.x provides a compatibility module that allows you to access TensorFlow 1.x functionality. This is the most straightforward solution if you need to maintain compatibility with older code.
import tensorflow as tf
# Instead of tf.logging (which causes the error)
# Use the compatibility module
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.INFO)
tf.compat.v1.logging.info("This is a log message")Output:
INFO:tensorflow:This is a log messageI executed the above example code and added the screenshot below.

This approach is particularly useful when you’re transitioning from TensorFlow 1.x to 2.x but don’t want to rewrite all your logging code.
Check out ModuleNotFoundError: No module named ‘tensorflow.keras.utils.np_utils’
Method 2: Use Python’s Built-in Logging Module
TensorFlow 2.x encourages the use of Python’s standard logging module instead of its custom logging implementation. This is a more maintainable approach in the long run.
import logging
# Configure the logging module
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
# Create a logger
logger = logging.getLogger('tensorflow_application')
# Use the logger
logger.info('Starting model training')
logger.warning('Memory usage is high')
logger.error('Failed to load data')Output:
2025-06-13 16:47:20,227 - tensorflow_application - INFO - Starting model training
2025-06-13 16:47:20,227 - tensorflow_application - WARNING - Memory usage is high
2025-06-13 16:47:20,227 - tensorflow_application - ERROR - Failed to load dataI executed the above example code and added the screenshot below.

This method gives you more flexibility and is the recommended approach for new TensorFlow 2.x projects.
Raed ModuleNotFoundError: No Module Named ‘keras.utils.vis_utils’
Method 3: Update Your TensorFlow Code
If you’re working with code that uses the older logging API, you’ll need to update it to use the appropriate methods in TensorFlow 2.x. Here’s a quick reference for migrating logging code:
| TensorFlow 1.x | TensorFlow 2.x |
|---|---|
tf.logging.set_verbosity(tf.logging.INFO) | tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.INFO) |
tf.logging.info("message") | tf.compat.v1.logging.info("message") or logging.info("message") |
tf.logging.warn("message") | tf.compat.v1.logging.warn("message") or logging.warning("message") |
For example, if you have code like this:
# Old code (TensorFlow 1.x)
import tensorflow as tf
tf.logging.set_verbosity(tf.logging.INFO)
tf.logging.info("Starting training")You would update it to:
# New code (TensorFlow 2.x)
import tensorflow as tf
import logging
# Option 1: Using tf.compat.v1
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.INFO)
tf.compat.v1.logging.info("Starting training")
# Option 2: Using Python's logging (preferred)
logging.basicConfig(level=logging.INFO)
logging.info("Starting training")Check out ModuleNotFoundError: No module named ‘tensorflow.python.keras’
Method 4: Check Your TensorFlow Version
Sometimes the error occurs because you’re using a different TensorFlow version than you think. You can check your current version with:
import tensorflow as tf
print(tf.__version__)I executed the above example code and added the screenshot below.

If you’re using a very old version of TensorFlow (like 0.x), you might need to upgrade to access newer features or use a different logging approach entirely.
Read AttributeError: module ‘tensorflow’ has no attribute ‘count_nonzero’
Method 5: Install the Correct Version of TensorFlow
If you’re following a tutorial that uses tf.logging and you don’t want to update the code, you can install TensorFlow 1.x instead:
# Uninstall current TensorFlow
pip uninstall tensorflow tensorflow-gpu
# Install TensorFlow 1.15 (the last 1.x release)
pip install tensorflow==1.15.0However, I recommend updating your code to use TensorFlow 2.x when possible, as it has significant improvements and is the actively maintained version.
Check out AttributeError: module ‘tensorflow’ has no attribute ‘reduce_sum’
Method 6: Use TensorFlow’s Newer Logging Alternatives
TensorFlow 2.x offers several alternatives to the old logging system:
Use TensorBoard for Visualized Logging
import tensorflow as tf
import datetime
# Create a TensorBoard callback
log_dir = "logs/fit/" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
tensorboard_callback = tf.keras.callbacks.TensorBoard(
log_dir=log_dir,
histogram_freq=1
)
# Use the callback when training your model
model.fit(
x_train, y_train,
epochs=10,
validation_data=(x_test, y_test),
callbacks=[tensorboard_callback]
)This approach not only logs information but also provides visualizations that can be viewed with TensorBoard.
Read Solve AttributeError: module ‘tensorflow’ has no attribute ‘py_function’
Use tf.print for Debugging
For quick debugging, you can use tf.print:
import tensorflow as tf
# Create a simple model
x = tf.constant([[1, 2], [3, 4]])
y = tf.square(x)
# Print the result during execution
tf.print("The result is:", y)This is useful for seeing values during graph execution.
I’ve found that using a combination of Python’s logging module for application-level logging and TensorBoard for model metrics gives the best results in modern TensorFlow applications.
Check out oduleNotFoundError: No module named ‘tensorflow.keras.layers’
Real-World Example: Stock Price Prediction Model
Let’s put this into context with a real example. Imagine you’re building a stock price prediction model for companies in the S&P 500:
import tensorflow as tf
import numpy as np
import pandas as pd
import logging
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger('stock_predictor')
# Load S&P 500 stock data
logger.info("Loading historical S&P 500 data")
try:
df = pd.read_csv('sp500_stocks.csv')
logger.info(f"Successfully loaded data with {len(df)} records")
except Exception as e:
logger.error(f"Failed to load stock data: {str(e)}")
raise
# Prepare data for model
logger.info("Preparing data for TensorFlow model")
# ... data preparation code ...
# Build and train model
logger.info("Building LSTM model for stock prediction")
# ... model building code ...
logger.info("Model training complete")This approach uses Python’s standard logging module, which is more versatile and won’t cause the “no attribute ‘logging'” error.
Read AttributeError: Module ‘tensorflow’ has no attribute ‘placeholder’
Avoid the Error in Future Projects
To prevent this error in future projects:
- Always check the TensorFlow version compatibility when using code from tutorials
- Use Python’s built-in logging for general application logs
- Use TensorBoard for model training metrics and visualization
- Keep your TensorFlow skills updated as the API evolves
When working with a team, establish clear logging standards at the beginning of the project to ensure everyone uses the same approach.
I hope this article has helped you solve the “AttributeError: Module ‘tensorflow’ has no attribute ‘logging'” issue. As TensorFlow continues to evolve, staying updated with the latest best practices will help you avoid similar errors in the future.
You may like to read:
- AttributeError: Module ‘tensorflow’ has no attribute ‘sparse_tensor_to_dense’
- Module ‘tensorflow’ has no attribute ‘sparse_placeholder’
- Module ‘tensorflow’ has no attribute ‘optimizers’

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.