Import keras.datasets not working
Asked Answered
U

3

11

I have keras installed on my linux machine, but when I try to import a dataset from the keras.datasets I get an error that it cannot find it.

So for example:

from keras.datasets import mnist

I get the error

ImportError: No module named keras.datasets

I installed keras using pip install and it installed successfully.

Under answered 16/2, 2016 at 11:33 Comment(3)
Do you have multiple Python versions? For which one did you installed keras?Bienne
Thanks good question. Indeed I have multiple versions of python, but I installed it for 2.7 and tried it out with 2.7 too.Under
And if I look in the folder with the packages, it is there also: /usr/local/lib/python2.7/dist-packages/keras/datasets$ ls cifar100.py cifar10.py cifar.py data_utils.py imdb.py __init__.py mnist.py reuters.py cifar100.pyc cifar10.pyc cifar.pyc data_utils.pyc imdb.pyc __init__.pyc mnist.pyc reuters.pycUnder
N
2

Indeed the problem was that I had multiple versions of Python.

Removing Anaconda Python and installing all libraries using pip / apt-get instead of conda solved my problem.

I found this to be true but it is not necessary to delete anaconda. I had the same issue but with multiple python versions. However, i created an environment that only used the Anaconda version (while in that environment). In terminal (on mac and other suitable terminals), type/copy

conda create -n dataweekends python=2.7 pandas scikit-learn jupyter matplotlib

dataweekends is simply the name of the environment you created. To access this, just use the command

source activate dataweekends

Be mindful that you might (probably) have to reinstall dependencies once in that new environment.

I got this trick from here "https://www.dataweekends.com/blog/2017/03/09/set-up-your-mac-for-deep-learning-with-python-keras-and-tensorflow"

I would also recommend setting up different environments for each project you do in python.

Nash answered 27/8, 2017 at 16:33 Comment(0)
S
2

Do you have keras.py or keras.pyc in the current working directory? If so, this will mess up the imports. Try renaming the file and/or deleting keras.pyc.

Soandso answered 5/3, 2017 at 14:24 Comment(2)
No I didn't. That seems like a very bad thing to have.Under
saved my day!! I named my file as 'keras.py' and got the same problemDecanal
N
2

Indeed the problem was that I had multiple versions of Python.

Removing Anaconda Python and installing all libraries using pip / apt-get instead of conda solved my problem.

I found this to be true but it is not necessary to delete anaconda. I had the same issue but with multiple python versions. However, i created an environment that only used the Anaconda version (while in that environment). In terminal (on mac and other suitable terminals), type/copy

conda create -n dataweekends python=2.7 pandas scikit-learn jupyter matplotlib

dataweekends is simply the name of the environment you created. To access this, just use the command

source activate dataweekends

Be mindful that you might (probably) have to reinstall dependencies once in that new environment.

I got this trick from here "https://www.dataweekends.com/blog/2017/03/09/set-up-your-mac-for-deep-learning-with-python-keras-and-tensorflow"

I would also recommend setting up different environments for each project you do in python.

Nash answered 27/8, 2017 at 16:33 Comment(0)
U
0

Thanks to the comment from Selcuk which got me on the right track.

Indeed the problem was that I had multiple versions of Python. I followed some online instructions to installing Keras which recommended installing MiniConda/Conda/Anaconda which is its own version of python. So I had two Python2.7 versions installed:

  • Normal Linux Python 2.7
  • Anaconda Python 2.7

Removing Anaconda Python and installing all libraries using pip / apt-get instead of conda solved my problem.

Under answered 16/2, 2016 at 13:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.