6
votes

I was running Jupyter Notebook and the following error occurs

ModuleNotFoundError
Traceback (most recent call last) in ---->

from keras.models import Sequential

from keras.layers import (
Conv2D, MaxPooling2D, Flatten, Dense, Dropout)

ModuleNotFoundError: No module named 'keras'

I have tried using import sys; sys.path and found this

['/home/xxx/notebook',
 '/home/xxx/anaconda3/lib/python37.zip',
 '/home/xxx/anaconda3/lib/python3.7',
 '/home/xxx/anaconda3/lib/python3.7/lib-dynload',
 '',
 '/home/xxx/anaconda3/lib/python3.7/site-packages',
 '/home/xxx/anaconda3/lib/python3.7/site-packages/IPython/extensions',
 '/home/xxx/.ipython']

Is there any problem with the installation? Do I need to reinstall everything from python to anaconda. Would some be able to point me to a proper installation of anaconda

BTW, if u have installed python, should you install python package through anaconda again

Thanks

5
Did you actually install keras and tensorflow in anaconda?Dr. Snoopy
It is possible that you have more than one version of python on your machine, and Keras has been installed on a version different from the version running the Jupyter. Did you install keras and jupyter both via anaconda?alift
@Matias Valdenegro: I am sorry but I vaguely have the believe that they are packaged together with anaconda. Does keras and tensorflow comes packaged with anaconda?user1538798
You need to install themDr. Snoopy
@ alift before installing anaconda, I have installed python v2. afterwhich i have installed anacondauser1538798

5 Answers

13
votes

You have to install all the dependencies first before using it. Try using

  • conda install tensorflow
  • conda install keras

by installing it with conda command it manage your versions compatibility with other libraries.

with pip install libraries will only install in your current environment and the latest version of the library sometimes latest libraries are not compatible with the other libraries so we have to take care of version compatibility.

10
votes

keras is actually part of tensorflow so all you have to do is just

from tensorflow.keras.models import Sequential

from tensorflow.keras.layers import Dense,Dropout,Activation, Flatten, Conv2D, MaxPooling2D

cheers mate

1
votes

Create a virtual environment and install all packages and specially jupyter-notebook in it. Some times it is necessary to install jupyter-notebook in each virtual environment to work properly with other libraries. It is preferred to use anaconda.

After creating your virtual env use this command to install jupyter:

conda install -c anaconda jupyter
0
votes

If you have installed all the required packages in a virtual/conda environment, have you tried saving the environment as an ipython kernel? I got those errors when I tried to launch a jupyter notebook from my virtual environment but I hadn't explicitly created a kernel for it.

https://ipython.readthedocs.io/en/stable/install/kernel_install.html

0
votes

Install packages in Anaconda Navigator -> Enviroments -> Play button -> Open Terminal -> conda install keras