Keras: ImportError: No module named 'keras.models'; 'keras' is not a package

Created on 15 Sep 2015  路  12Comments  路  Source: keras-team/keras

I have install all below dependency before installing keras.

numpy, scipy
pyyaml
Theano
HDF5 and h5py.

Still I am getting following error if I try to import keras module.

Traceback (most recent call last):
File "", line 2195, in _find_and_load_unlocked
AttributeError: 'module' object has no attribute '__path__'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "", line 1, in
File "/home/ttpc/keras.py", line 10, in
from keras.models import Sequential

I am using Ubuntu 14.04. Below is trace of my pip packages.
Cython==0.23.2
Keras==0.1.3
Pillow==2.3.0
PyYAML==3.11
Theano==0.7.0
beautifulsoup4==4.2.1
chardet==2.2.1
colorama==0.2.5
command-not-found==0.3
decorator==3.4.0
h5py==2.2.1
html5lib==0.999
language-selector==0.1
lxml==3.3.3
matplotlib==1.3.1
nose==1.3.1
numexpr==2.2.2
numpy==1.8.2
pandas==0.13.1
pycurl==7.19.3
pygobject==3.12.0
pyparsing==2.0.1
python-apt==0.9.3.5ubuntu1
python-dateutil==2.0
pytz==2012c
requests==2.2.1
scikit-learn==0.16.1
scipy==0.13.3
six==1.5.2
tables==3.1.1
tornado==3.1.1
ufw==0.34-rc-0ubuntu2
unattended-upgrades==0.1
urllib3==1.7.1
wheel==0.24.0
xgboost==0.4

Kindly help me out.

Most helpful comment

Your script seems to be named keras.py, causing python to think that is where you want to import from. Just rename your script and it should work.

All 12 comments

Maybe a PATH / PYTHONPATH issue? Just reinstall everything from scratch. Make sure that the Python you're calling is the same as the Python to which you're installing packages with pip (especially if you installed Anaconda).

In any case, this is not a Keras-related issue.

Your script seems to be named keras.py, causing python to think that is where you want to import from. Just rename your script and it should work.

you were spot on phreeza, indeed it was silly mistake. :)

I need urgent help!

I get this error from my code:

Traceback (most recent call last):
File "/Users/Zahra/code1", line 1, in
from keras.models import Sequential
ImportError: No module named keras.models

What does that mean?
I installed Anaconda, btw.

@zahrasorour

check your keras.json file in ~/.keras/ directory is exists.

hello friends
i have the same problem can you help me to know how to solve it
mportError Traceback (most recent call last)
in ()
1 #with keras
----> 2 from keras.models import Sequential
3 from keras.layers import Dense

ImportError: No module named keras.models

The solution for me was:
$pip3 install tensorflow

May be obvious, but I also got this issue when I named my Python file keras.py. File needs to be named something other than the package name.

File "test.py",

line 4, in from keras.datasets import mnist ModuleNotFoundError: No module named 'keras'

Your keras library maybe under tensorflow:

import like this:

from tensorflow.keras.models import Sequential

first neural network with keras make predictions

from numpy import loadtxt

from keras.models import Sequential

from tensorflow.keras.models import Sequential
from keras.layers import Dense

load the dataset

dataset = loadtxt('pima-indians-diabetes.csv', delimiter=',')

split into input (X) and output (y) variables

X = dataset[:,0:8]
y = dataset[:,8]

define the keras model

model = Sequential()
model.add(Dense(12, input_dim=8, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(1, activation='sigmoid'))

compile the keras model

model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
'''# fit the keras model on the dataset
model.fit(X, y, epochs=150, batch_size=10, verbose=0)'''

fit the keras model on the dataset without progress bars

model.fit(X, y, epochs=150, batch_size=10, verbose=0)

evaluate the keras model

_, accuracy = model.evaluate(X, y, verbose=0)

make class predictions with the model

predictions = model.predict_classes(X)

summarize the first 5 cases

for i in range(5):
print('%s => %d (expected %d)' % (X[i].tolist(), predictions[i], y[i]))

I am also getting the same error. I am using Linux

Your keras library maybe under tensorflow:

import like this:

from tensorflow.keras.models import Sequential

Thanks dude馃!!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

anjishnu picture anjishnu  路  3Comments

KeironO picture KeironO  路  3Comments

vinayakumarr picture vinayakumarr  路  3Comments

farizrahman4u picture farizrahman4u  路  3Comments

LuCeHe picture LuCeHe  路  3Comments