modulenotfounderror: no module named 'keras'

The ModuleNotFoundError: No module named 'keras' is a common error that Python programmers face while working with deep learning libraries. It simply means that Python cannot find the Keras module in the system.

In this Answer, we will learn how to resolve this issue.

Understanding the error

When we encounter this error, it signifies that the Keras library is not installed in our Python environment. In Python, libraries are collections of functions and methods that allow us to perform various actions without writing our code.

Note: To learn more about building neural networks using Keras, please refer to this Answer.

Let's first understand how to reproduce this error. If we try to import Keras in our Python script or shell without having it installed, we will encounter this error:

import keras

Having understood the error, let's delve into its solution!

Prerequisites

Before commencing with the solution, it is essential to ensure that Python and pip (Python package installer) are installed in the system.

To verify if Python is installed, use the following command.

python --version
Command to check the Python version

To check the installation of pip, use the following command.

pip --version
Command to check the pip version

If Python or pip is not installed, it is necessary to install them before proceeding.

Installing Keras using pip

The most direct resolution is to install Keras using pip. The command for this is:

pip install keras
Command to install Keras using pip

This command will extract the Keras library from the Python Package Index (PyPI)PyPI (Python Package Index) is a repository that hosts a collection of software packages developed by the Python community, making it easy for users to find, install, and manage Python libraries and applications. and install it into the system.

Once the installation is complete, return to the Python script or shell, and try importing Keras again.

import keras

If Keras was installed successfully, this command should now execute without errors.

Frequent issues and their solutions

Despite following the aforementioned steps, some issues might persist. Let's review some common ones and their respective solutions.

TensorFlow not installed

If TensorFlow, the primary backend for Keras, is not installed in the environment, the error can occur. Even though Keras might be properly installed, the absence of TensorFlow can still lead to this error because Keras depends on it for its operations.

To resolve this issue, TensorFlow must be installed in the same environment as Keras.

Install TensorFlow using pip by entering the following command.

pip install tensorflow
Command to install TensorFlow

Incompatible Keras version

At times, the version of Keras might not be compatible with the version of Python. In such scenarios, upgrading Keras to the latest version could be beneficial. Use the command below to upgrade

pip install --upgrade keras
Command to upgrade Keras

Python environment misconfiguration

If Keras and TensorFlow are installed, but Python cannot find them, the Python environment might need to be checked. Python uses a list of directories known as sys.path, to determine where to look for modules to import. The directories in sys.path can be checked with the following.

import sys
print(sys.path)

The directory where Keras is installed should be listed in sys.path. If Keras is installed globally, it should be in a directory such as /usr/local/lib/python3.6/site-packages. If it's installed in a virtual environment, the path will resemble /path_to_environment/lib/python3.6/site-packages.

Operating in a virtual environment

If we're operating within a virtual environment, it is crucial to confirm that Keras is installed within that environment. The libraries installed on the system's Python are unavailable in the virtual environment.

To install Keras in the virtual environment, activate the environment.

source myenv/bin/activate # for Unix or macOS
myenv\Scripts\activate # for Windows
Commands to activate the virtual environment

Then, install Keras:.

pip install keras
Command to install Keras using pip

Multiple Python installations

Multiple Python installations on a system could cause confusion, as the version used to run the script might not be the same as the one used to install Keras and TensorFlow. The Python installation running the script can be verified using sys.executable.

import sys
print(sys.executable)

This command will print the path to the Python executable that's running the script. If this isn't the same Python used to install Keras and TensorFlow, they must be installed for the correct Python version.

Pip-Python version mismatch

In cases where multiple versions of Python are installed on the system, the pip command might install packages for a different version than the one used in the script. In such cases, it's advised to use the pip associated with the relevant Python version. If Python 3 is in use, use pip3.

pip3 install keras
Command to install Keras using pip3

Conclusion

With the steps outlined above, Keras will be installed and functional in the Python environment. This method of installing libraries applies to others as well, so if a similar error with another library crops up, these steps could be followed.

Copyright ©2024 Educative, Inc. All rights reserved