Recently, I was working on a complex neural network model for a client in Chicago when I ran into a frustrating roadblock. As I executed my Python script, I was greeted with this error:
ModuleNotFoundError: No module named 'tensorflow.python.keras'This error stopped my project dead in its tracks. If you’re encountering the same issue, I understand your frustration.
Over my 12 years of Python development, I’ve dealt with this particular error numerous times, especially when working with TensorFlow and Keras.
In this article, I will share the most effective solutions I’ve discovered to fix this problem and get your machine learning projects back on track.
Understand the Root Cause
Before getting into solutions, let’s understand why this error occurs. The error typically happens for one of these reasons:
- TensorFlow is not properly installed
- You’re using incompatible versions of TensorFlow and Keras
- Your Python environment has conflicting packages
- The import statement in your code is incorrect
- Your virtual environment isn’t activated
Let’s address each of these issues with practical solutions.
Solution 1: Install or Reinstall TensorFlow
The most common cause of this error is an incomplete TensorFlow installation. Here’s how to fix it:
# Uninstall existing TensorFlow (if any)
pip uninstall tensorflow tensorflow-gpu
# Install the latest version
pip install tensorflowFor specific versions, you can specify the version number:
pip install tensorflow==2.8.0I recently had to downgrade to TensorFlow 2.4.1 for a project with the New York Department of Transportation that required compatibility with older CUDA drivers.
Read AttributeError: Module ‘keras.backend’ has no attribute ‘get_session’
Solution 2: Use the Correct Import Statement
The import error often happens because of changes in TensorFlow’s structure across versions. In newer versions (TensorFlow 2.x), the correct import statement is:
from tensorflow import kerasNote:
from tensorflow.python.keras import keras # This will cause the errorIf you’re working with older code that was written for TensorFlow 1.x, you’ll need to update your import statements.
Check out Fix Module ‘TensorFlow’ has no attribute ‘session’
Solution 3: Create a Clean Virtual Environment
Package conflicts are a common source of this error. I always recommend using virtual environments for machine learning projects.
Here’s how I set up a clean environment for TensorFlow:
# Create a new virtual environment
python -m venv tf_env
# Activate it (Windows)
tf_env\Scripts\activate
# Activate it (macOS/Linux)
source tf_env/bin/activate
# Install TensorFlow
pip install tensorflowThis approach has saved me countless hours of debugging, especially when working on multiple projects with different dependency requirements.
Read Module ‘TensorFlow’ has no attribute ‘get_default_graph’
Solution 4: Check for Version Compatibility
TensorFlow and Keras version compatibility is crucial. Since TensorFlow 2.0, Keras has been integrated into TensorFlow, but this wasn’t always the case.
Here’s how to check your installed versions:
import tensorflow as tf
print(tf.__version__)
# For standalone Keras (if installed)
import keras
print(keras.__version__)You can see the output in the screenshot below.

If you’re using TensorFlow 2.x, you generally don’t need to install Keras separately. Having both standalone Keras and TensorFlow installed can cause conflicts.
Read AttributeError: Module ‘tensorflow’ has no attribute ‘logging’
Solution 5: Fix Package Installation Paths
Sometimes, Python can’t find modules due to installation path issues. This is particularly common in Windows or when using multiple Python installations.
Here’s a solution that works in most cases:
import sys
import os
# Print Python path to see where Python is looking for modules
print(sys.path)
# Add TensorFlow path manually if needed
tensorflow_path = os.path.join(os.path.dirname(sys.__file__), 'site-packages')
sys.path.append(tensorflow_path)
# Now try importing
from tensorflow import kerasYou can see the output in the screenshot below.

I had to use this approach when working on a project for a Boston financial firm that had a complex Python setup with multiple installations.
Check out AttributeError module ‘tensorflow’ has no attribute ‘summary’
Deal with Anaconda Environments
If you’re using Anaconda (which is popular for data science projects), the solution might be slightly different:
# Create a new conda environment
conda create -n tf_env python=3.8
# Activate it
conda activate tf_env
# Install TensorFlow through conda
conda install tensorflow
# Or via pip inside conda env
pip install tensorflowAnaconda’s package management system can sometimes resolve dependency issues more effectively than pip alone.
Read TensorFlow Convolution Neural Network
Special Case: GPU Support
If you’re working with TensorFlow GPU support (which many machine learning engineers in the USA do for performance reasons), the installation is more complex:
# Install TensorFlow with GPU support
pip install tensorflow-gpu
# Make sure CUDA and cuDNN are properly installedGPU compatibility issues can sometimes manifest as import errors. Ensure that your CUDA and cuDNN versions are compatible with your TensorFlow version.
Check out AttributeError: Module ‘tensorflow’ has no attribute ‘placeholder’
Test Your Installation
After applying any of the solutions above, verify that your installation works correctly:
import tensorflow as tf
from tensorflow import keras
# Create a simple model to test
model = keras.Sequential([
keras.layers.Dense(10, activation='relu', input_shape=(784,)),
keras.layers.Dense(10, activation='softmax')
])
print("TensorFlow and Keras are working!")If this code runs without errors, you’ve successfully resolved the issue.
Resolving the “ModuleNotFoundError: No module named ‘tensorflow.python.keras'” error typically involves ensuring proper installation, using correct import statements, and managing package dependencies.
These solutions have helped me countless times with projects ranging from computer vision systems for retailers in Dallas to natural language processing models for tech startups in San Francisco.
Remember that machine learning libraries like TensorFlow evolve rapidly, so what works today might need adjustment tomorrow. Always check the official documentation for the most up-to-date information.
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.