Fix the No module named keras error in Python

Fix the Modulenotfounderror no module named keras error

To fix the no module named keras exception, proceed as following:

  1. In your Python environment, open your command prompt or Anaconda prompt.
  2. Run the pip install keras command to install the library.
  3. If using Anaconda run conda install keras
  4. Close your command prompt and call keras from your Python script.

Why Python displays the ModuleNotFoundError for keras?

The keras library isn’t 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’). 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’ exception in Jupyter:

Solving 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.