modulenotfounderror: no module named ‘keras’ is one of the most annoying errors that so many machine learning and AI enthusiast have to come across. Some people have to try for days to just import Keras into their python code. It’s very frustrating and annoying indeed. Isn’t it annoying to fail at the very beginning of your project? There are so many things to be done using Keras, but what if you can’t even import it?
What is Keras?
This is for anyone just wondering here reading this post by chance. Keras in its simplest terms is a High-Level python neural network library that mainly runs on top of TensorFlow. In other terms, Keras provides a python interface/API for TensorFlow. It may sound confusing but TensorFlow and Keras go hand in hand. You probably know this if you have been working with ML and AI for a while.
Why do we get modulenotfounderror: no module named ‘keras’?
modulenotfounderror is raised when python cannot import a specified module successfully. This can be any module by the way. Having said that, if you are struggling with the error ModuleNotFoundError: No module named ‘sklearn’ read this blog post.
If you have typed the name Keras and the import command for Keras correctly, then there are few possibilities why you are seeing this error.
- You haven’t installed Keras successfully, It might be a incomplete installation or a failed one.
- You have installed Keras successfully but python interpreter cannot locate your Keras installation.
Python cannot locate where Keras has been installed, Hence raises the error modulenotfounderror: no module named ‘keras’
In this article, we aren’t going to talk about why Keras fails to install. Instead, we are talking about why python cannot locate and import an installed Keras module.
99% (well almost..) of the time, the cause of this error is that the python interpreter is unable to locate the Keras module.
Now let’s see the possible causes of these path mismatches and how to fix them.
You have installed keras in a different python virtual enviornment
Usually, what happens is that you have installed Keras and TensorFlow in a dedicated python virtual environment, and trying to execute your python code from another environment. If that is the case, modulenotfounderror: no module named ‘keras’ error can be fixed simply following the below instructions.
How to fix no module named keras error
To fix that you just have to change your running python program’s environment into the environment where Keras has been installed into.
If you are using PyCharm you can do this by editing the program environment in Run/Debug Configurations. In case if you are using a different IDE like spyder, there are similar methods to change your python project environment.
As we can see here my python project environment is set to the default python interpreter.
Now I have set my project environment to a virtual python environment called “TensorFlow”. It’s where I have successfully installed TensorFlow and Keras.
Let’s run the following lines of codes to confirm there’s no module named keras raised.
from tensorflow import keras print (keras.__version__)
Success! Keras was successfully imported into PyCharm.
Your Jupyter notebook cannot import Keras module hence raises modulenotfounderror: no module named ‘keras’
A lot of machine learning enthusiasts write their code in jupyter notebooks or in jupyter labs. And when it comes to installing keras and tensorflow, they often face no module named keras error.
The possible cause of these errors is again the different python virtual environments that are being used.
The jupyter notebook might have been installed in a different path than where the Keras and TensorFlow have been installed. So the copy of python’s interpreter in that environment is only going to check in its relevant location for any possible keras installation. You can check the python interpreter’s path by using sys.path in your jupyter notebook.
As you can see that I have installed jupyter with Anaconda.
But I have installed TensorFlow and Keras using conda package manager in a virtual environment called “tensorflow”. Having said that let me try to import tensorflow and keras into the current environment and see what’s going to happen.
As you can see, it can’t even find Tensorflow library.
How to fix no module named keras error
To fix this you can install jupyter in the same python virtual environment as your TensorFlow and Keras installation (In my case it’s named as “tensorflow“). To do that first you have to log into your virtual environment in CMD. Then install Jupyter notebook in that environment.
conda activate tensorflow
python -m pip install jupyter
Let’s open Jupyter notebook and run the same commands again, shall we?
While you are logged inside your virtual environment in CMD, type and enter the following command
Let’s get the system path of the current Python interpreter.
Now I will try again to import Keras and print its version.
from tensorflow import keras
Finally, there’s no modulenotfounderror: no module named ‘keras’! By the way, you can see that I have the most up to date version of the Keras which is version 2.5.0
Best Practices to Avoid modulenotfounderror: no module named ‘keras’
The best practice is to install all your machine learning and AI libraries and packages into a single environment. For example, you can install TensorFlow, Keras, Jupyter in one environment.
It’s a good practice to install Anaconda distribution on your computer. It will install most of the required machine learning and AI modules.
Then you can just install TensorFlow and Keras without creating any python environment (Though it isn’t recommended). You don’t have to install jupyter because Anaconda installs jupyter notebook by default. This way you can avoid no module named ‘keras’ error.
Or, as recommended, you can create a python environment and install TensorFlow and Keras. You can create virtual environments using conda within CMD or anaconda manager. Then if you are going to use jupyter notebooks as the development environment you can install jupyter in the same environment.
Then when you want to work with TensorFlow and Keras you can just log into that environment and proceed from there.
modulenotfounderror: no module named ‘keras’ can be quite frustrating. To fix this and successfully import Keras into your code can be different according to the situation.
But you can simply avoid no module named keras error by being aware of your python interpreter’s path and the different virtual environments you have used.
Because as you may be aware by now this error raises when the path of Keras is not properly set according to your python project.