ShNBl84
ShNBl84

Reputation: 55

Tensorflow tutorial error in first step: "Cannot import name 'keras'"

I am new to Python and am trying to learn Tensorflow. I installed Tensorflow yesterday (so it should be updated) via a new environment on Anaconda. The first step in the Tensorflow tutorial is:

import tensorflow as tf
from tensorflow import keras

The result I am getting is:

ImportError                               Traceback (most recent call last)
<ipython-input-13-3d1e6d42ad48> in <module>
      1 import tensorflow as tf
----> 2 from tensorflow import keras

ImportError: cannot import name 'keras'

How do I get keras to work so I can start the tutorial?

Computer is Windows 10.

I looked through stackoverflow and saw similar questions, but not this specific one. I do not know the version because when I do:

import tensorflow as tf
print(tf.version)

I get:

AttributeError                            Traceback (most recent call last)
<ipython-input-16-7380a45e29ab> in <module>
      1 import tensorflow as tf
----> 2 print(tf.version)

AttributeError: module 'tensorflow' has no attribute 'version'

Upvotes: 1

Views: 7413

Answers (3)

ShNBl84
ShNBl84

Reputation: 55

Steps I took:

downloaded jupyter to my tensorflow environment

updated some packages

downloaded matplotlib to the tensorflow environment.

It seems to be working now. Thank you to the team effort to help a noob!

Upvotes: 0

Filipe Aleixo
Filipe Aleixo

Reputation: 4244

You don't need to import tensorflow from keras. You actually don't even need to import tensorflow. Keras uses tensorflow as backend by default.

You can do for example:

import keras as k

Although this is not recommended, as you will be importing the whole library. So you generally would only import the packages you need. For instance:

from keras.models import Sequential

If you want to access tensorflow from keras you can do:

import keras.backend as K

And then you can access tensorflow's methods from K

Upvotes: 2

user8207612
user8207612

Reputation:

Once check the version of Python that you are using.

In the anaconda prompt type python --version and check it. If it shows 3.7.0, then tensorflow won't work since python 3.7 doesn't support tensorflow as of now.

In that case, you have to create a new environment in anaconda and install a version of python that supports tensorflow (like Python 3.6). You can follow this link for the same.

Upvotes: 0

Related Questions