Have you ever been excited to start a machine learning project using TensorFlow and Keras, only to be stopped in your tracks by the dreaded “ModuleNotFoundError: No module named ‘tensorflow.keras'” error? I’ve encountered this frustrating issue many times throughout my decade-plus Python development career, and I understand how it can disrupt your workflow.
In this article, I will walk you through several proven methods to resolve this error so you can get back to building your machine learning models.
Let’s get started..
Understand the Error
When Python displays “ModuleNotFoundError: No module named ‘tensorflow.keras'”, it simply means it can’t find the Keras module within TensorFlow. This can happen for several reasons:
- TensorFlow is not installed
- You have an outdated TensorFlow version
- You’re using the wrong import statement
- Your virtual environment configuration has issues
Let’s address each of these potential causes with practical solutions.
Read ModuleNotFoundError: No module named ‘tensorflow.keras.utils.np_utils’
Methods to Fix ModuleNotFoundError: No module named ‘tensorflow.keras’ in Python
Now, I will explain how to fix the ModuleNotFoundError: No module named ‘tensorflow.keras’ in Python.
1: Install or Upgrade TensorFlow
The most common reason for this error is that TensorFlow is either not installed or you’re using an outdated version. Keras has been integrated into TensorFlow since version 2.0, so older versions might cause this error.
Here’s how I fix this:
# Install TensorFlow with pip
pip install tensorflow
# Or upgrade to the latest version
pip install --upgrade tensorflowFor those who prefer Conda:
conda install tensorflowAfter installation, verify your TensorFlow version:
import tensorflow as tf
print(tf.__version__)You can see the output in the screenshot below.

Make sure it displays version 2.0 or higher. If it shows an older version, you’ll need to upgrade.
Check out ModuleNotFoundError: No Module Named ‘keras.utils.vis_utils’
2: Correct Your Import Statement
Another common cause is using the wrong import statement. Since TensorFlow 2.0, the recommended way to import Keras is through the TensorFlow package.
# Correct import for TensorFlow 2.x
from tensorflow import keras
# Then you can use it like this
model = keras.Sequential([
keras.layers.Dense(128, activation='relu'),
keras.layers.Dense(10, activation='softmax')
])If you’re working with older code that used the standalone Keras library, you’ll need to update your imports.
3: Install Standalone Keras (For Legacy Code)
If you’re working with older code that specifically requires the standalone Keras library, you can install it separately:
pip install kerasThen import it directly:
import kerasHowever, I strongly recommend migrating to the TensorFlow integration of Keras for better compatibility with newer libraries and features.
Read ModuleNotFoundError: No module named ‘tensorflow.python.keras’
4: Check Your Virtual Environment
Sometimes the error occurs because you’re running Python in a virtual environment where TensorFlow isn’t installed. This is a common issue I’ve experienced when switching between projects.
To check if you’re in a virtual environment:
import sys
print(sys.prefix)You can see the output in the screenshot below.

If you’re using a virtual environment, make sure to install TensorFlow within that environment:
# Activate your virtual environment first
# For Windows
venv\Scripts\activate
# For macOS/Linux
source venv/bin/activate
# Then install TensorFlow
pip install tensorflow5: Resolve Package Conflicts
Occasionally, package conflicts can cause this error. I’ve found that creating a fresh environment often resolves these issues:
# Create a new conda environment with TensorFlow
conda create -n tf_env tensorflow
conda activate tf_env
# Or with virtualenv
python -m venv tf_env
source tf_env/bin/activate # On Windows: tf_env\Scripts\activate
pip install tensorflowThis approach gives you a clean slate without any package conflicts.
Check out AttributeError: module ‘tensorflow’ has no attribute ‘count_nonzero’
6: Fix Path Issues
Sometimes Python can’t find TensorFlow because of PATH issues. You can check your Python path and make sure it includes the location where TensorFlow is installed:
import sys
print(sys.path)You can see the output in the screenshot below.

If needed, you can add the path manually:
import sys
sys.path.append('/path/to/tensorflow/installation')However, this is usually a temporary fix, and I recommend resolving the underlying installation issue instead.
Read AttributeError: module ‘tensorflow’ has no attribute ‘reduce_sum’
Real-World Example: Build a Stock Price Predictor
Let’s apply what we’ve learned to a real-world example. Imagine we’re building a stock price predictor for major U.S. companies like Apple, Google, and Amazon:
# First, make sure TensorFlow is properly installed
import tensorflow as tf
print(f"TensorFlow version: {tf.__version__}")
# Import Keras from TensorFlow
from tensorflow import keras
from tensorflow.keras import layers
# Now we can build our stock price prediction model
model = keras.Sequential([
layers.LSTM(50, return_sequences=True, input_shape=(60, 1)),
layers.LSTM(50, return_sequences=False),
layers.Dense(25),
layers.Dense(1)
])
model.compile(optimizer='adam', loss='mean_squared_error')
print("Model built successfully!")If you’re able to run this code without errors, congratulations! You’ve successfully resolved the “ModuleNotFoundError: No module named ‘tensorflow.keras'” issue.
Read Solve AttributeError: module ‘tensorflow’ has no attribute ‘py_function’
Troubleshoot GPU Support
If you’re working with large datasets like financial time series, you might want TensorFlow with GPU support. This requires additional setup:
# Check if GPU is available
print("GPU Available: ", tf.config.list_physical_devices('GPU'))If this returns an empty list, you may need to install the GPU version of TensorFlow and configure your drivers properly.
After years of working with TensorFlow, I’ve found that solving the “No module named ‘tensorflow.keras'” error is usually simple once you understand the root cause. The most common fix is simply installing or upgrading TensorFlow to version 2.0 or higher and using the correct import statement.
Remember that the machine learning ecosystem evolves rapidly, so always check the official TensorFlow documentation for the most current recommendations. With these solutions in hand, you can get back to building sophisticated machine learning models for your data analysis projects or business applications.
TensorFlow-Related tutorials:
- ModuleNotFoundError: No module named ‘tensorflow.keras.layers’
- TensorFlow Convolution Neural Network
- AttributeError: Module ‘tensorflow’ has no attribute ‘placeholder’

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.