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.
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:
If you are following code written for standalone Keras, then the project may expect the separately installed keras package:
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:
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:
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:
Then import it like this:
If your project explicitly expects standalone Keras, install that package too:
And verify the direct import:
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:
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:
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.kerasor standalonekeras. - Verify the active interpreter with
sys.executableand install packages withpython -m pip. - Use
from tensorflow import kerasfor 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.pythat can shadow the real package.
Related reading
- ImportError No module named 'matplotlib' -- Using Anaconda tensorflow environment
- ImportError No module named tensorflow
- ImportError No module named 'tensorflow.core
- ImportError No module named 'tensorflow.python
- ImportError No module named sklearn.cross_validation
- ImportError No module named 'sklearn.lda
- ImportError No module named matplotlib.pyplot
- ImportError No module named 'MySQL
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.