3

I created a new conda env with

conda create --name tf tensorflow=2.6

and tried to compile

import tensorflow as tf
model = tf.keras.models.Sequential()

resulting in ModuleNotFoundError: No module named 'keras'

conda install keras

doesn't change anything. I could go with

from tensorflow.keras.models import Sequential
model = Sequential()

but when I pip install tensorflow-addons and

from tensorflow_addons.seq2seq.sampler import TrainingSampler

I end up with the same error

uninstalling tensorflow, installing just keras and trying

from keras.models import Sequential
model = Sequential()

results in the same error

my versions are

tensorflow = 2.6
keras = 2.6
tensorflow-adons = 0.14
SzymonO
  • 432
  • 3
  • 15
  • try `from tensorflow.keras .models import Sequential` – Avishka Dambawinna Oct 27 '21 at 06:29
  • @AvishkaDambawinna I tried but I need to use tensorflow_addons. Read the full question – SzymonO Oct 27 '21 at 06:37
  • Please checkout these threads . https://stackoverflow.com/questions/45271344/importerror-no-module-named-keras https://www.edureka.co/community/67388/modulenotfounderror-no-module-named-keras –  Nov 18 '21 at 04:47
  • I faced the same problem. I had to downgrade tensorflow to 2.5 then it worked. The only command I used is `conda create --name tf tensorflow=2.5`, no need to install anything else. – Gqqnbig Dec 18 '21 at 17:27
  • @Gqqnbig turns out that I had 2 keras versions installed, one from tensorflow and one from tensorflow nightly. Would be wise to check your `conda list` and `pip list` for duplicate keras installations – SzymonO Dec 19 '21 at 18:08

2 Answers2

0

What turned out is that I had both keras and keras nightly installed, the problem got resolved after uninstalling keras-nightly. If anyone encounters this check your conda list and pip list for duplicate keras installations

SzymonO
  • 432
  • 3
  • 15
0

I had a similar problem so adding an anwser here in the hope of helping others.

I use anaconda to set up python environments for machine learning precisely to get away from dependency clashes. So I set up a new environment to use tensorflow-gpu (version 2.6). After setup, I had the same problem importing keras on my initial tests. I was surprised because my understanding was keras is included under tensorflow. I was disinclined to downgrade or upgrade since the conda tensorflow install with the CUDA backend took over 2.5 hours. So I simply installed keras as follows:

conda install keras

The install was short and sweet and seems to have fixed the issue. I now see keras in my env listing and the imports work...

>>> import tensorflow as tf
>>> print(tf.keras.__version__)
2.6.0