Python
ImportError
Keras
Troubleshooting
Machine Learning

ImportError No module named 'keras'

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

ImportError: No module named 'keras' almost always means your Python interpreter is not looking at the environment you think it is, or your code is importing the wrong Keras package for that project. The fix is usually simple once you separate installation issues from import-path issues.

Understand Which Keras Your Project Uses

There are two common patterns in modern Python machine learning code:

  • 'from tensorflow import keras'
  • 'import keras'

They are not always interchangeable. Many TensorFlow-based projects use the Keras API bundled with TensorFlow, so the correct import is:

python
from tensorflow import keras

If you are following code written for standalone Keras, then the project may expect the separately installed keras package:

python
import keras

The important part is matching the import statement to the environment and dependency set your project actually uses.

Verify the Active Python Environment

Before installing anything, confirm which interpreter is running your script:

bash
python -c "import sys; print(sys.executable)"
python -m pip --version

Those two commands should point to the same environment. If they do not, you may be installing packages into one interpreter and running code with another.

Now check what is already installed:

bash
python -m pip show tensorflow
python -m pip show keras

If neither package appears, Python is behaving correctly: the module really is not installed in that environment.

Install the Right Dependency

For TensorFlow-backed projects, installing TensorFlow is often enough because tf.keras ships with it:

bash
python -m pip install tensorflow

Then import it like this:

python
from tensorflow import keras

print(keras.__version__)

If your project explicitly expects standalone Keras, install that package too:

bash
python -m pip install keras tensorflow

And verify the direct import:

python
import keras

print(keras.__version__)

Using python -m pip instead of plain pip is important because it ties installation to the exact interpreter you just tested.

Watch for Environment Mismatches

Virtual environments, Conda environments, IDE interpreters, and notebook kernels are all frequent sources of confusion. A package can be installed correctly and still look missing if the running process is attached to a different interpreter.

For example, in Jupyter:

python
import sys
print(sys.executable)

If that path does not match the environment where you installed Keras or TensorFlow, switch the notebook kernel instead of reinstalling packages repeatedly.

The same rule applies to VS Code, PyCharm, and command-line shells. Always verify the active interpreter before assuming installation failed.

Check for Local Name Collisions

Sometimes the error is not really about installation at all. Python imports can be shadowed by your own files or folders. A script named keras.py, a folder named keras, or even a stale keras.pyc file in the working directory can interfere with module resolution.

This is a quick check:

bash
ls

If you see a local file or package named keras, rename it and try again.

Prefer Consistent Imports Across the Project

Mixing import keras in one file and from tensorflow import keras in another is a good way to create confusing dependency behavior. Pick one style that matches the project and keep it consistent.

For TensorFlow-heavy codebases, from tensorflow import keras is often the safer choice because it makes the backend relationship explicit. If you are building against standalone Keras APIs deliberately, keep that explicit too.

Common Pitfalls

The most common mistake is installing packages with one Python interpreter and running the code with another. Always compare sys.executable with python -m pip --version.

Another pitfall is blindly installing keras when the project actually expects tensorflow.keras. That may remove the import error without fixing compatibility expectations.

It is also easy to overlook notebook kernels and IDE interpreter settings. In those environments, the terminal and the running code are often not using the same Python.

Finally, local files named keras.py can shadow the real package and produce misleading import errors. Check the project directory before assuming packaging is the problem.

Summary

  • First decide whether the project should use tensorflow.keras or standalone keras.
  • Verify the active interpreter with sys.executable and install packages with python -m pip.
  • Use from tensorflow import keras for TensorFlow-backed projects unless the project explicitly depends on standalone Keras.
  • Check IDE and notebook interpreter selection before reinstalling anything.
  • Look for local filename collisions such as keras.py that can shadow the real package.

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.