-1

I've been trying to import keras from tensorflow using the following statement:

import tensorflow as tf
from tensorflow import keras

Tensorflow has been updated, it should work as far as I know but I still get the following message:

from tensorflow import keras
ImportError: cannot import name 'keras' from 'tensorflow'(/Users/gabork/PycharmProjects/Tester/venv/lib/python3.7/site-packages/tensorflow/__init__.py)

SOLUTION: I was using Python 3.7 which seems like recent TensorFlow versions do not support and on the other hand, older versions of TensorFlow do not support Keras. For now I’ll either downgrade Python or just import keras.

Greg. O Hajdu
  • 126
  • 2
  • 14

2 Answers2

1

I would try import keras and see if that works.

Greg. O Hajdu
  • 126
  • 2
  • 14
  • This seems to work but I would still be interested why the other way doesn't. – Greg. O Hajdu Sep 04 '18 at 11:36
  • This does neither answer the question nor resolve the issue, since by doing this you import of course the stand-alone Keras, not the Tensorflow-included one (surprised that it is accepted by the OP @C.Szasz ) – desertnaut Sep 04 '18 at 15:46
  • @desertnaut Because it turns out it was a compatibility problem with different versions, importing keras or downgrading will resolve my issue. – Greg. O Hajdu Sep 04 '18 at 15:53
  • @C.Szasz you mean you didn't know that the option `import keras` was available in the first place? – desertnaut Sep 04 '18 at 15:57
  • 1
    @desertnaut I apologize that I’m not familiar with the hierarchy of these libraries. I was following a tutorial on TensorFlow, got an error message, tried researching it, asked a question and now I learnt something. Do you have a better solution if I may ask? – Greg. O Hajdu Sep 04 '18 at 15:59
0

You are using TensorFlow 0.12.0, that is too old, keras was included in a newer version to the one you have, that's why you get an import error.

So either you update TensorFlow (highly recommended), or install keras separately.

Dr. Snoopy
  • 55,122
  • 7
  • 121
  • 140
  • Interesting. When I update in the terminal, it is version 1.10.1 but inside PyCharm when I try to update it simply says: `Requirement already up-to-date: tensorflow in ./venv/lib/python3.7/site-packages (0.12.0)` – Greg. O Hajdu Sep 04 '18 at 11:50
  • @C.Szasz You simply have two TF version installed, one inside a virtualenv and another in python (without virtualenvs). – Dr. Snoopy Sep 04 '18 at 11:55
  • @matias-valdenegre Alright! Now I tried updating virtualenv following Tensorflow instructions and I get `Could not find a version that satisfies the requirement tensorflow (from versions: ) No matching distribution found for tensorflow` – Greg. O Hajdu Sep 04 '18 at 12:10