Select Page

How to Solve Python ModuleNotFoundError: no module named ‘keras’

by | Machine Learning, Programming, Python, TensorFlow, Tips

A common error you may encounter when using Python is modulenotfounderror: no module named ‘keras’.

Keras comes packaged with Tensorflow 2.0 as tensorflow.keras. To import and start using Keras, you need to install TensorFlow 2.

You can install TensorFlow 2 using the following commands

# Ensure you have the latest version of pip

python3 -m pip install --upgrade pip

# Install TensorFlow 

python3 -m pip install tensorflow

Once you have tensorflow installed, you can import Keras using import tensorflow.keras or from tensorflow import keras.

This tutorial goes through the exact steps to troubleshoot this error for the Windows, Mac and Linux operating systems.


ModuleNotFoundError: no module named ‘keras’

What is Keras?

Keras is a deep learning API written in Python that runs on top of the machine learning platform TensorFlow. The simplest way to install Keras is to use the package manager for Python called pip and install TensorFlow 2.0.

How to install TensorFlow on Windows Operating System

Keras comes packaged with TensorFlow 2.0. Therefore we do not need to install Keras directly. The following set of instructions is for installing TensorFlow.

You need to download and install Python on your PC. Ensure you select the install launcher for all users and Add Python to PATH checkboxes. The latter ensures the interpreter is in the execution path. Pip is automatically on Windows for Python versions 2.7.9+ and 3.4+.

Install Microsoft Visual C++ Redistributable

Once you have Python installed, you need to install the Microsoft Visual C++ Redistributable for Visual Studio 2015, 2017, and 2019.

  1. Go to the Microsoft Visual C++ downloads.
  2. Scroll down the page to the Visual Studio 2015, 2017 and 2019 section.
  3. Download and install the Microsoft Visual C++ Redistributable for Visual Studio 2015, 2017 and 2019 for your platform.

Install Miniconda

Use the following command to install Miniconda. During the installation, you will be required to press enter and type “yes:

curl https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -o Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh

After this step, you may need to restart your terminal or source ~/.bashrc to enable the conda command. Use conda -V to verify that the installation was successful.

Create a conda environment

Next, you will need to create a new conda environment. Let’s call the environment tf.

conda create --name tf python=3.9

You can activate the environment using the following command:

conda activate tf

You must remain in this environment for the remainder of the installation.

GPU Setup

Skip this part if you only want to run TensorFlow on CPU. First, ensure that you have the NVIDIA GPU driver installed. Then, you can install CUDA and cuDNN with conda as follows:

conda install -c conda-forge cudatoolkit=11.2 cudnn=8.1.0

Install pip

You can install pip on Windows by downloading the installation package, opening the command line and launching the installer. You can install pip via the CMD prompt by running the following command.

python get-pip.py

You may need to run the command prompt as administrator. Check whether the installation has been successful by typing.

pip --version

Install TensorFlow

TensorFlow requires a recent version of pip, you can upgrade your pip installation using the –upgrade flag as follows

pip install --upgrade pip

To install TensorFlow with pip, run the following command from the command prompt.

python3 -m pip install tensorflow

Verify installation

You can verify the CPU setup with the following command:

python3 -c "import tensorflow as tf; print(tf.reduce_sum(tf.random.normal([1000, 1000])))"

If the command returns a tensor, then you have installed TensorFlow successfully.

You can verify the GPU setup with the following command:

python3 -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))"

If the command returns a list of GPU devices, then you have installed TensorFlow successfully.

How to install Keras on Mac Operating System

Open a terminal by pressing command (⌘) + Space Bar to open the Spotlight search. Type in terminal and press enter. To get pip, first ensure you have installed Python3.

You can install Python3 by using the Homebrew package manager:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

export PATH="/usr/local/opt/python/libexec/bin:$PATH"

# if you are on macOS 10.12 (Sierra) use `export PATH="/usr/local/bin:/usr/local/sbin:$PATH"`

brew update

brew install python  # Python 3
python3 --version
Python 3.8.8

Download pip by running the following curl command:

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py

The curl command allows you to specify a direct download link. Using the -o option sets the name of the downloaded file.

Install pip by running:

python3 get-pip.py

To install tensorflow, first, create the virtual environment:

python3 -m venv env

Then activate the environment using:

source env/bin/activate 

You will see “env” in parenthesis next to the command line prompt. You can install tensorflow within the environment by running the following command from the command prompt.

python3 -m pip install tensorflow

How to install Keras on Linux Operating System

All major Linux distributions have Python installed by default. However, you will need to install pip. You can install pip from the terminal, but the installation instructions depend on the Linux distribution you are using. You will need root privileges to install pip. Open a terminal and use the commands relevant to your Linux distribution to install pip.

Installing pip for Ubuntu, Debian, and Linux Mint

sudo apt install python-pip3

Installing pip for CentOS 8 (and newer), Fedora, and Red Hat

sudo dnf install python-pip3

Installing pip for CentOS 6 and 7, and older versions of Red Hat

sudo yum install epel-release

sudo yum install python-pip3

Installing pip for Arch Linux and Manjaro

sudo pacman -S python-pip

Installing pip for OpenSUSE

sudo zypper python3-pip

Tensorflow Installation on Linux

Install Miniconda

Use the following command to install Miniconda. During the installation, you will be required to press enter and type “yes:

curl https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -o Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh

After this step, you may need to restart your terminal or source ~/.bashrc to enable the conda command. Use conda -V to verify that the installation was successful.

Create a conda environment

Next, you will need to create a new conda environment. Let’s call the environment tf.

conda create --name tf python=3.9

You can activate the environment using the following command:

conda activate tf

You must remain in this environment for the remainder of the installation

TensorFlow requires a recent version of pip, you can upgrade your pip installation using the –upgrade flag as follows

pip install --upgrade pip

To install TensorFlow with pip, run the following command from the command prompt.

python3 -m pip install tensorflow

You can verify the CPU setup with the following command:

python3 -c "import tensorflow as tf; print(tf.reduce_sum(tf.random.normal([1000, 1000])))"

If the command returns a tensor, then you have installed TensorFlow successfully.

You can verify the GPU setup with the following command:

python3 -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))"

If the command returns a list of GPU devices, then you have installed TensorFlow successfully.

Check Keras Version

Once you have successfully installed TensorFlow, you can check the version of Keras you are using the __version__ attribute.

from tensorflow import keras
print(keras.__version__)
2.4.0

Importing Functions from Keras

Let’s look at an example of importing the Keras API from TensorFlow and instantiating a trained vision model ResNet50:

from tensorflow import keras

from tensorflow.keras import layers

vison_model = keras.applications.ResNet50()
Downloading data from https://storage.googleapis.com/tensorflow/keras-applications/resnet/resnet50_weights_tf_dim_ordering_tf_kernels.h5
102973440/102967424 [==============================] - 15s 0us/step

Summary

Congratulations on reading to the end of this tutorial!

For further reading on installing data science and machine learning libraries, you can go to the articles:

Go to the online courses page on Python to learn more about Python for data science and machine learning.

Have fun and happy researching!