27

TensorFlow 2.8 was recently released and I installed it as soon as it was out. I really need it for support of higher NumPy versions and a few new features. However, after installing it in my conda environment with

python3 -m pip install --upgrade tensorflow

neither PyCharm nor VSCode can no longer resolve the import from tensorflow.keras import ....

The imports themselves seem to work at runtime, but because the import cannot be resolved I can't make use of code completion, visualizing signatures of functions and etc. Has anybody encountered a similar issue?

everything was working with TF 2.7 - the version I had before.

Note: I'm using Python 3.8

Vs Code enter image description here

PyCharm enter image description here

I tried to check the versions through the PyCharm interpreter tab and this is what I saw. For some reason PyCharm isn't aware that there are versions after 2.0 (I have the latest version of pip installed in that environment). I'm guessing this is related, but not sure what to do with that.

enter image description here

djvaroli
  • 1,223
  • 1
  • 11
  • 28

8 Answers8

30

I had the same problem and resolved it by importing it as

from tensorflow.python.keras.layers import Dense
buddemat
  • 4,552
  • 14
  • 29
  • 49
Filip BRSJAK
  • 311
  • 2
  • 7
  • 5
    Only few statements work in this fashion. I tried `from tensorflow.keras.preprocessing.image import ImageDataGenerator` and `from tensorflow.python.keras.preprocessing.image import ImageDataGenerator` and neither of them worked for me. – user9113784 Mar 19 '22 at 06:22
  • Which Python interpreter do you use? I use Python 3.8 – Filip BRSJAK Mar 22 '22 at 10:54
  • worked for me ! How do you know to do this *t ?! tensorflow.python.keras import layers – David Mar 24 '22 at 04:02
  • 1
    You mean the format of the comment? Comments use mini-Markdown formatting: [link](http://example.com) _italic_ **bold** `code`. The post author will always be notified of your comment. To also notify a previous commenter, mention their user name: @peter or @PeterSmith will both work. Learn more about formatting… – Filip BRSJAK Mar 24 '22 at 20:02
  • I have switched from working on my local machine to Google Collab and I use the following imports: ```python import mlflow\ import mlflow.keras\ import mlflow.tracking\ from mlflow import pyfunc\ from mlflow.models import Model\ import numpy as np\ import pandas as pd\ from matplotlib import pyplot as plt\ from keras.layers import LSTM\ from keras.layers import Dense\ from keras.models import Sequential\ from sklearn.preprocessing import MinMaxScaler\ ``` – Filip BRSJAK Dec 22 '22 at 12:06
16

This is a bug in the current version of tensorflow, as discussed in this issue.

You can work around it by either

  1. modifying the file site-packages/tensorflow/__init__.py as described in this answer from the referenced issue or
  2. using import keras.api._v2.keras as keras since this seems to be the exact package tensorflow loads itself. (Though you need to reference the protected member _v2 here, which is against python conventions.)

The reason here is that tensorflow tries to load the keras module in a lazy fashion, which means that it holds only a reference to the module until the module is used. Only then the keras module will be actually loaded. Therefore IDEs only know about the reference tensorflow holds to the keras module and not its content.

TomS
  • 336
  • 3
  • 5
  • 1
    It seems to be a different problem. `import tensorflow; tensorflow.keras` and `import tensorflow.keras`(or `from tensorflow.keras import ...`) are resolved differently by IDE. The second one is based on `tensorflow.__path__`, while the first one is based on `tensorflow.keras`. – YouJiacheng Jul 28 '22 at 03:42
  • See https://github.com/tensorflow/tensorflow/pull/54104#issuecomment-1067102133 – YouJiacheng Jul 28 '22 at 04:05
9

I see the problem in Google Colab as well. Although running the code works just fine. It's just an IDE complaint that supposedly it cannot find the imports. Very strange. I hope someone from the TensorFlow team gives feedback soon.

Could not be resolved error

Jazon Samillano
  • 151
  • 1
  • 4
1

Resolving

import tensorflow
foo = tenstorflow.keras.foo
# if foo is a submodule but not an attribute, this will fail

and

from tensorflow.keras import foo
# if foo is an attribute, this is (roughly) equivalent to
import tensorflow.keras
foo = tenstorflow.keras.foo
# if foo is a submodule but not an attribute, this is (roughly) equivalent to
import tensorflow.keras.foo as foo

are different.

The first one need tensorflow has keras attribute with correct type statically during type checking.

But the second one need tensorflow.__path__ contains keras module statically during type checking.

BTW, for from tensorflow import keras: If tensorflow has keras attribute, then it uses the attribute, otherwise it import keras as a submodule.

Theoretically, the second one should only work for 2.2.0 <= TF < 2.6.0, which has tensorflow/keras folder. Because tensorflow/keras should be removed in TF 2.6 according to TF 2.6 Release Log, otherwise from tensorflow import keras(tensorflow.keras backed by the keras PIP package) will be different from import tensorflow.keras as keras(tensorflow/keras backed by tensorflow/python/keras).

In fact, however, the second one works for 2.2.0 <= TF < 2.8.0, since tensorflow/keras is not removed until TF 2.8. Interestingly, tensorflow/python/keras is not removed yet (Release 2.9.1), violating the claim in TF 2.6 Release Log that "... will be removed in future release (2.7)".

The first one is broken for TF >= 2.5.0 because of keras lazy loading introduced in TF 2.5, and haven't been fixed yet (Release 2.9.1) though related commits have been merged into master branch.

See https://github.com/tensorflow/tensorflow/pull/54104 and https://github.com/tensorflow/tensorflow/commit/e65b68a0914408118995d2f8b55c4286859362f8

See also https://github.com/tensorflow/tensorflow/pull/54104#issuecomment-1067102133

YouJiacheng
  • 449
  • 3
  • 11
0

This one worked for me:

from keras.utils.np_utils import to_categorical
robni
  • 904
  • 2
  • 6
  • 20
0

You can create a symlink in tensorflow directory pointing to keras sources like below:

cd ./virtualenvs/myenv/lib/python3.x/site-packages/tensorflow
ln -s ../keras/api/_v2/keras/ keras
Mehraban
  • 3,164
  • 4
  • 37
  • 60
0

This error typically occurs when there is a naming conflict in TensorFlow while trying to import Keras. It can be resolved by modifying the __init__.py file in the TensorFlow package. Follow the steps below to resolve the issue:

  1. Locate the __init__.py file within your TensorFlow installation. The file path is typically site-packages/tensorflow/__init__.py.

    Alternatively, you can quickly open the file by clicking on the word "tensorflow" in your import statement and pressing CTRL (or CMD on Mac) simultaneously. This should navigate you directly to the __init__.py file.

  2. Open the __init__.py file in a text editor.

  3. Look for the section of code around line 387 that mentions the keras module.

  4. Replace this existing code block

    _keras_module = "keras.api._v2.keras"
    keras = _LazyLoader("keras", globals(), _keras_module)
    _module_dir = _module_util.get_parent_dir_for_name(_keras_module)
    if _module_dir:
      _current_module.__path__ = [_module_dir] + _current_module.__path__
    setattr(_current_module, "keras", keras)
    
  5. with the following modified code:

    import typing as _typing
    if _typing.TYPE_CHECKING:
      from keras.api._v2 import keras
    else:
      _keras_module = "keras.api._v2.keras"
      keras = _LazyLoader("keras", globals(), _keras_module)
      _module_dir = _module_util.get_parent_dir_for_name(_keras_module)
      if _module_dir:
        _current_module.__path__ = [_module_dir] + _current_module.__path__
      setattr(_current_module, "keras", keras)
    
  6. Save the changes to the __init__.py file.

Now, when you import Keras from TensorFlow, the naming conflict should be resolved. This modification allows the correct import of the Keras module within TensorFlow.

this works for me.

Marawan Mamdouh
  • 584
  • 1
  • 6
  • 15
0
import tensorflow as tf
keras = tf.keras
models = tf.keras.models
...

It works well for auto completion.

CHIYOI
  • 11
  • 1
  • 3