Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImportError: No module named 'keras'

So basically, I am fairly new to programming and using python. I am trying to build an ANN model for which I have to use Tensor flow, Theano and Keras library. I have Anaconda 4.4.1 with Python 3.5.2 on Windows 10 x64 and I have installed these libraries by following method.

  1. Create a new environment with Anaconda and Python 3.5: conda create -n tensorflow python=3.5 anaconda
  2. Activate the environment: activate tensorflow
  3. After this you can install Theano, TensorFlow and Keras: conda install theano, conda install mingw libpython, pip install tensorflow, pip install keras,
  4. Update the packages: conda update --all

All these packages are installed correctly and I have check them with conda list. However, when I am trying to import any of these 3 libraries (i.e. Tensor flow, Theano and Keras), it is giving me the following error:

Traceback (most recent call last):
File "<ipython-input-3-c74e2bd4ca71>", line 1, in <module>
import keras
ImportError: No module named 'keras'
like image 334
Atif Mehmood Avatar asked Jul 24 '17 01:07

Atif Mehmood


People also ask

How do I fix No module named keras?

Hence, you can resolve this by uninstalling the keras module and installing it back again. Below is the code that will help in installation and uninstallation. It is because your TensorFlow module might be installed in an environment that is different from the environment where Keras is installed in.


9 Answers

Hi I have an solution try this if you are using Anaconda-Navigator

go to Anaconda Environment and search keras package and then install.

install keras

enter image description here

after install just type import keras in shell its working.

enter image description here

like image 112
vipin Avatar answered Oct 02 '22 17:10

vipin


Have you tried using keras documentation

Install Keras from PyPI (recommended):

Note: These installation steps assume that you are on a Linux or Mac environment. If you are on Windows, you will need to remove sudo to run the commands below.

sudo pip install keras

If you are using a virtualenv, you may want to avoid using sudo:

pip install keras

from: https://keras.io/

like image 20
user13384405 Avatar answered Oct 02 '22 19:10

user13384405


Now you need to have Tensorflow installed and then write, for example:

import tensorflow as tf
...
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Dense(12, input_dim=8, activation='relu'))
model.add(tf.keras.layers.Dense(8, activation='relu'))
model.add(tf.keras.layers.Dense(1, activation='sigmoid'))
 ...

Works for Tensorflow version: 2.4.1.

Or just type:

import tensorflow as tf
from tensorflow import keras
...
like image 45
coding Avatar answered Oct 02 '22 17:10

coding


Try

import sys
print(sys.path)

and see if your anaconda site-packages folder is in the list.

It should be something like WHERE_YOU_INSTALLED_ANACONDA\anaconda3\envs\ENVIRONMENT_NAME\lib\python3.5\site-packages

If the path setting is correct, then try listing the folder content, and see if Keras, TensorFlow and Theano are in this folder.

like image 45
Yu-Yang Avatar answered Oct 02 '22 17:10

Yu-Yang


I ran into a very similar issue after switching computers and downloading the latest Anaconda, which comes with python 3.6. It was no problem to install python 3.5 in its own environment, and install keras to this environment, but import keraskept failing.

My inelegant solution (assuming you've already got tensorflow/theano/cntk working fine in your global environment)?

Move the keras folder installed to Anaconda3/envs//Lib/site-packages/keras to Anaconda3/Lib/site-packages/keras. Now import keras gives a depreciation warning when run from a jupyter notebook launched via start menu, but it does work, and correctly returns the backend keras is running on.

like image 21
Mark Bailey Avatar answered Oct 02 '22 18:10

Mark Bailey


I spent the whole day to install Keras, tried all the available methods online, almost dying. But I still cannot import keras.

(1). After using conda install or pip install, and delete the "1 > null > 2&1" ... I activated in conda prompt by activating tensorflow_cpu, it doesn't work anyway.

(2). Then checked the keras, and print os.path(), no virtual environment inside. I got so braindead, just copied all the keras data file from virtual environment env, and put into the "C:\Users\Administrator\Anaconda3\Lib\site-packages".

(3). Now, tensorflow and keras work well.

like image 26
Jaden Avatar answered Oct 02 '22 18:10

Jaden


Click Update Index and then try searching for Keras again.

like image 22
Danny Meyer Avatar answered Oct 02 '22 18:10

Danny Meyer


A direct and simple way to fix it is as below, #uninstall keras and tensorflow

pip uninstall keras
pip uninstall tensorflow

#Now install keras and tensorflow for required version with dependencies.

pip install keras==2.2.4
pip install tensorflow==1.13.1

Always make sure that you install right version of tensorflow which supports that keras version as well, else you may end up in trouble again. By the way , the above fix worked for me.

like image 1
carrycooldude Avatar answered Oct 02 '22 18:10

carrycooldude


I solved this problem by running one of the following in the terminal according to anaconda website.

To install this package (keras) with conda run one of the following:

  • conda install -c conda-forge keras conda install -c
  • conda-forge/label/broken keras conda install -c
  • conda-forge/label/cf201901 keras conda install -c
  • conda-forge/label/cf202003 keras

If you never use conda before you can check anaconda.

like image 1
Mohamed Abdelrazek Avatar answered Oct 02 '22 19:10

Mohamed Abdelrazek