Import "tensorflow.keras" could not be resolved after upgrading to TensorFlow 2.8.0

Viewed 18182

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

7 Answers

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.

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

from tensorflow.python.keras.layers import Dense

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

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

This has been a pattern as this post in GitHub shows. I'm getting the same. Ignoring it since the code still runs, but would rather not have the yellow. I hope someone from tensorflow can chime in. :)

This one worked for me:

from keras.utils.np_utils import to_categorical

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
Related