Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to import Keras(from TensorFlow 2.0) in PyCharm 2019.2

I have just installed the stable version of TensorFlow 2.0 (released on October 1st 2019) in PyCharm.

The problem is that the keras package is unavailable. Unable to import keras

The actual error is :

"cannot import name 'keras' from tensorflow"

I have installed via pip install tensorflow==2.0.0 the CPU version, and then uninstalled the CPU version and installed the GPU version , via pip install tensorflow-gpu==2.0.0.

Neither of the above worked versions of TensorFlow were working properly(could not import keras or other packages via from tensorflow.package_X import Y).

If I revert TensorFlow to version 2.0.0.b1, keras is available as a package (PyCharm recognises it) and everything runs smoothly.

Is there a way to solve this problem? Am I making a mistake in the installation process?

UPDATE --- Importing from the Python Console works and allows the imports without any error. Writing from the console works

like image 464
Timbus Calin Avatar asked Oct 01 '19 16:10

Timbus Calin


People also ask

Is keras compatible with TensorFlow 2?

Installation & compatibility To start using Keras, simply install TensorFlow 2. Keras/TensorFlow are compatible with: Python 3.7–3.10.


Video Answer


2 Answers

For PyCharm Users

For those who use PyCharm. Install future (EAP) release 2019.3 EAP build 193.3793.14 from here. With that, you will be able to use autocomplete for the current stable release of TensorFlow (i.e. 2.0). I have tried it and it works :).

For other IDEs

For users with other IDEs, this will be resolved only after the stable version is released, which is anyways the case now. But this might take some more time for a fix. See the comment here. I assume it will be wise to wait and keep using version 2.0.0.b1. On the other hand avoid imports from tensorflow_core if you do not want to refactor your code in the future.

Note: for autocomplete to work use import statement as below

import tensorflow.keras as tk

# this does not work for autocomplete 
# from tensorflow import keras as tk  

The autocomplete works for TensorFlow 2.0.0 on CPU version, but the autocomplete does not work for the GPU version.

like image 143
Praveen Kulkarni Avatar answered Sep 22 '22 10:09

Praveen Kulkarni


SOLVED --- See the answers to this problem below.

SOLUTION 1 (best solution)

Is the accepted answer provided above. It works on EAP version, I tested it on several machines with Windows.

SOLUTION 2

Although PyCharm does not recognise the modules, running the .py file works. I still do not know if this is a problem of TensorFlow or PyCharm, but this is the solution that I have found, many people have run into this problem.

PyCharm does not see the imports, considers them error

enter image description here

SOLUTION 3

Import the modules from tensorflow_core instead of tensorflow

Example: from tensorflow_core.python.keras.preprocessing.image import ImageDataGenerator

However, as mentioned by @Nagabhushan S N in the comment below and above in the accepted answer:

On the other hand avoid imports from tensorflow_core if you do not want to refactor your code in the future.

enter image description here

like image 41
Timbus Calin Avatar answered Sep 19 '22 10:09

Timbus Calin