Fix the No module named keras error in Python

In this tutorial we will learn to troubleshoot import errors related to the keras Deep Learning library. You might encounter this error when trying to call one of the library modules such as engine.topology, api, preprocessing, applications etc’ from your Python program.

Below is an example of No modulenamed ‘keras’ in Jupyter:

Why Python displays the ModuleNotFoundError for keras?

The keras library is shipped as part of the Python standard library. Same goes for the machine learning libraries such as tensorflow or sklearn and the popular Data Analysis libraries such as pandas, numpy and matplotlib. Therefore, keras might be first installed in your Python development environment before you can call it from your IDE (PyCharm, Jupyter Notebooks, Spyder, VSCode etc’).

How to fix the no module named keras error?

Anaconda / Miniconda

  • From your Python IDE save and close your Python program.
  • On Windows open the conda command prompt. On macOS / Linux open your terminal window.
  • Type the following command
conda activate <your_conda_env_path>

Note: if you are unsure about your conda environment name and path you can find it first by running the following command in the conda prompt:

conda info --envs

After your environment is active, you can go ahead and type:

conda install keras

After installation is succesful you can reopen your IDE and call your required keras library module.

Manually configured environments

In this case you will need to call directly the pip ( Python Package installer) utility.

  • Save your work and close your Python development tool.
  • Open the windows command prompt (Windows +R then type cmd) or your terminal in macOS and Linux.
  • Navigate to the path of your python environment (if not sure – search for the location of the python.exe)
  • Type the following command:
pip install keras
  • Once the installation finishes open your IDE and keep coding.